javascript - React Native:单击选项卡时重新渲染组件
问题描述
我正在使用 React Navigation 并试图getData()
在单击选项卡时强制重新渲染组件(和函数)。
我试图遵循这一点,但没有运气。
路由器
export const Tabs = TabNavigator(
{
Progress: {
screen: ProgressStack,
navigationOptions: ({ navigation }) => ({
tabBarLabel: 'Progress',
tabBarIcon: ({ tintColor }) =>
<Icon name="trending-up" size={25} color={tintColor} />
}),
},
export const ProgressStack = StackNavigator({
Progress: {
screen: ProgressScreen,
navigationOptions: ({ navigation }) => ({
title: 'Progress',
}),
},
});
进度组件
componentWillReceiveProps() {
console.log('rerender!');
this.getData();
}
解决方案
我没有尝试过,但我认为这可能是正确的方法:https ://reactnavigation.org/docs/with-navigation-focus.html
使用那个高阶组件传递一个isFocusedProp
然后使用componentDidUpdate
调用getData
componentDidUpdate (previousProps) {
if (!previousProps.isFocused && this.props.isFocused) {
this.getData()
}
}
您还可以使用 onPress 按钮事件:https ://reactnavigation.org/docs/tab-navigator.html#tabbaronpress ,但这需要一种方法来触发您正在执行的数据调用。如果您使用的是 redux,则需要通过路由参数传递调度或绑定的操作函数。
推荐阅读
- visual-studio-code - 每当我尝试将所选文本包含在花括号中时,它都会被包含在单引号中,这非常烦人
- spring - 如果我只使用 RestController,它仍然是 Spring MVC 吗?
- express - CORS 问题:预检请求不允许重定向
- flutter - Flutter Android Studio(医生检查崩溃)
- javascript - HMAC - 通过 Postman 进行 SHA256 身份验证
- node.js - ReferenceError:FirebaseFirestore 未定义 Firebase 函数
- powerbi - 从另一个数据集 Power BI 的记录中搜索列
- python - 在 manage.py runserver 之后 Python Django 错误“没有名为 ...”的模块。我是否正确组织了我的文件?
- bash - 在 bash 脚本中,如何引用文件作为交互式提示的输入
- javascript - 未在 javascript 中定义未捕获的引用错误