【发布时间】:2018-06-11 18:09:12
【问题描述】:
我在我正在构建的 React Native 应用程序中使用React Navigation,并且我有一组位于 TabNavigator 中的屏幕。其中一个屏幕可能会更改 AsyncStorage 中的某些内容,这些内容需要反映在另一个屏幕中。
我尝试了各种不同的方法,包括在第二个屏幕的navigationOptions 中设置tabBarOnPress 属性,但这不起作用。我也尝试使用componentDidUpdate() 和componentWillUpdate(),但没有。
公认的方法是什么?
更新 1 我设法让我在按下选项卡时捕获它
static navigationOptions = {
headerTitle: 'Screen B',
tabBarLabel: 'Screen B',
tabBarOnPress: (obj) => {
// load data here....
// this causes this screen to actually appear
obj.jumpToIndex(obj.scene.index)
}
};
现在,我需要在 using 中加载数据
loadFavouritesData() {
// load from AsyncStorage here and update state
}
但是,在 tabBarOnPress 的箭头函数中使用 this.loadFavouritesData() 并没有这样做,因为它不是正确的 this。之后我能做什么?
【问题讨论】:
-
我说对了吗?你有屏幕 A,它可以更新异步存储中的一些数据,屏幕 B,它在渲染时引用这些数据。当用户从 A 导航到 B 时,您希望组件 B 使用新数据重新渲染
-
@NikitaIsaev 是的,差不多。
-
屏幕B的
componentWillMount()呢?它应该在组件出现在屏幕上时触发。 -
componentWillUpdate()仅在 props 或 state 发生更改时触发,事实并非如此。componentWillMount()应该在您的导航器将其呈现到 UI 对象模型时触发。 -
是的,
componentWillMount()仅在屏幕初始加载时触发一次(未显示)。
标签: javascript react-native react-navigation asyncstorage tabnavigator