return 到页面时尝试重置数据
Trying to reset data when return to the page
当return到页面
时,我正在尝试重置数据
这是我的一段代码
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)
我把上面的代码也放在了上一页。但错误显示相同。
你能把'willFocus'改成'focus'吗,如下图所示,然后检查
我将 willfocus 更改为 didfocus 并将其删除 componentWillUnMount 。成功了
componentDidMount() {
....
this.focusListener= this.props.navigation.addListener(
'didFocus',
() => {
this.setState({products:[],alreadyLoadedCount:0});
// loading page code
}
);
}
componentWillUnmount() {
this.focusListener.remove();
}
当return到页面
时,我正在尝试重置数据这是我的一段代码
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)
我把上面的代码也放在了上一页。但错误显示相同。
你能把'willFocus'改成'focus'吗,如下图所示,然后检查
我将 willfocus 更改为 didfocus 并将其删除 componentWillUnMount 。成功了
componentDidMount() {
....
this.focusListener= this.props.navigation.addListener(
'didFocus',
() => {
this.setState({products:[],alreadyLoadedCount:0});
// loading page code
}
);
}
componentWillUnmount() {
this.focusListener.remove();
}