reactjs - 为什么这段代码不调用 componentDidUpdate() ?
问题描述
代码
const Index = {
screen: IndexScreen,
params: { category: null },
};
const IndexStack = createStackNavigator(
{
Index,
},
{
initialRouteName: 'Index',
navigationOptions: {
...
},
},
);
<TouchableOpacity
onPress={() => this.props.navigation.navigate('Index', { category: 1})}
>
<Text>Navigating to Index</Text>
</TouchableOpacity>
产品.js
async componentDidUpdate(prevProps) {
console.log(prevProps.navigation.state.params);
}
问题
从其他组件导航到索引时,它传递一个参数:类别。
索引应该收到它,但console.log()
什么componentDidUpdate()
都不返回。
解决方案
在您的情况下,您应该获取导航参数componentDidMount
如您在此处看到的,在渲染后componentDidUpdate
触发,因此当您第一次启动此组件时它不会工作。
推荐阅读
- python - Python:使用 anacoda3 安装 python 3 后将 python2 设置为默认值
- php - cron 中的 PHP 正在将所有输出记录到邮件中,但我只想在出现错误时记录
- javascript - React JS 应用程序的浏览器中出现“用户不允许访问 Windows 位置”错误
- python - 如何使用点积组合 2 个数据帧
- php - MySQL中的Auto_Increment和PHP中的PDO问题
- c - 如何在我的 C 程序中修复“复制:错误的文件描述符”
- python-3.x - Pytorch 加载的模型给出不一致的结果
- c# - 如何在动态创建的选项卡项中显示列表框中的项目
- javascript - 如果元素属于数组 Firestore
- python - 根据第一个元素搜索元组列表并获取第二个元素值列表