Search code examples
androidreactjsreact-nativereact-hooksreact-lifecycle

I want to change state of the component without reloading metro in react-native


so basically i have bunch of apps in my application as you can see in image In my app it check if application is installed or not, if its installed its shows open and if not it shows install now the issue i'm facing is if application is not installed, i click on the button install, it will redirect to play-store then i will install, now whenever i came to my main page, without reloading metro, the title of button is not updating to open. example. :-
now the last app, i have already installed but title of button is not changed yet. when i reload the metro it will show open as title, but i want to, without reloading metro, i want change button title.

import React, { Component } from 'react';
    import { Text, Image ,View, Button, Linking } from 'react-native'
    import * as ApkManager from 'react-native-apk-manager';
    import styles from './styles'
    
    class A extends Component {
         constructor(props){
         super(props)
           this.state = {
              title: "Install",
              buttonStatus: false
           }
       }
      
       componentDidMount() {
         console.log("state has been mounted")
          ApkManager.isAppInstalled(this.props.props.packageName).then((data)=> {
          if(data == true){
             this.setState({
                 title: "Open"
             })
          }
        });
       }
    render(){
            return(
             <View style={styles.container}>
              <Image source={{ uri: this.props.props.imageIcon}}
                     style={{width: 100, height: 100, margin: 10}}/>
               <Button style={styles.button}
                  title={this.state.title} 
                  onPress={() => {
                      if(this.state.title == 'Install'){
                        Linking.openURL(this.props.props.storeUrl)
                      }else{
                             this.state.title = 'Open'
                             ApkManager.openApk(this.props.props.packageName);
                    }
                  }}
               />
            </View>
           )
        }
    }
    export default A

Solution

  • Use setinterval hook to repeat isInstalled method Apkmanager