返回页面时尝试重置数据

发布于 2025-01-14 08:30:51 字数 735 浏览 1 评论 0原文

我试图在返回页面时重置数据,

这是我的一段代码

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

    componentWillUnmount() {
        this.willFocusSubscription();
    }

,该页面完全重新加载,但问题是当我返回上一页时,以下错误显示

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

我也将上述代码放在上一页中。但错误显示相同。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的巷 2025-01-21 08:30:51

我将 willfocus 更改为 didfocus 并删除它 componentWillUnMount 。它起作用了

componentDidMount() {
   
....

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

componentWillUnmount() {
   this.focusListener.remove();
}

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();
}

柠栀 2025-01-21 08:30:51

您可以将 'willFocus' 更改为 'focus' 如下图所示并检查
输入图片此处描述

can you change 'willFocus' to 'focus' as show in the image below and check
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文