Search code examples
reactjsnative

Trying to reset data when return to the page


I am trying to reset data when return to the page

here is my piece of code

    componentDidMount(){
        
        this.checkIfAlreadyLoggedIn();
        
        this.willFocusSubscription = this.props.navigation.addListener(
            'willFocus',
            () => {
                this.setState({products:[],alreadyLoadedCount:0});
                this.checkIfAlreadyLoggedIn();
            }
          );
    }

    componentWillUnmount() {
        this.willFocusSubscription();
    }

the page is perfectly reload but the problem is when i back to the previous page the follwing error shows

 ERROR  TypeError: this.willFocusSubscription is not a function. (In 'this.willFocusSubscription()', 'this.willFocusSubscription' is an instance of Object)

i put the above code in the previous page also. but the error shows same.


Solution

  • I changed willfocus to didfocus and and remove it componentWillUnMount . it is worked

    componentDidMount() {
       
    ....
    
     this.focusListener= this.props.navigation.addListener(
                'didFocus',
                () => {
                    this.setState({products:[],alreadyLoadedCount:0});
                    // loading page code
                }
      );
    }
    
    componentWillUnmount() {
       this.focusListener.remove();
    }