刷新路线-仅在显示时显示(固定)

时间:2020-02-25 20:33:28

标签: react-native

我正在使用堆栈导航器,并且我有一个数据过多的屏幕,需要重新导航几次。 但是,介于两者之间的屏幕似乎缺乏性能,因为该数据仍在后台运行。

我尝试了以下操作,但是没有用:

 onLoad = () => {
    this.props.navigation.addListener('didFocus', () => console.log('x'))
    }

  componentDidMount() {
    this.onLoad()
  }

编辑:我通过使用navigation.dispatch(StackActions.reset)函数对其进行了修复

1 个答案:

答案 0 :(得分:0)

您可以使用react-navigation的HOC withNavigationFocus

只需导入

import {withNavigationFocus} from "react-navigation"

然后将其导出

export default withNavigationFocus(ComponentName)

然后,在您的componentDidUpdate内,检查一下isFocused道具并做您需要的事情。例如:

componentDidUpdate = prevProps => {
    if (this.props.isFocused !== prevProps.isFocused){ //If the focused state changed
    //do whatever you need to do like:
    if(this.props.isFocused === false) this.setState({stopLoadingData: false})
    else this.setState({stopLoadingData: false})
    }
}

您可以在docs

中了解更多信息
相关问题