react-native - 未调用 componentDidUpdate
问题描述
我有一个反应本机应用程序,我在 App.js 上调用 componentDidUpdate,但它没有触发。我想知道这是否是因为我是从 App.js 调用的?这是 App.js 文件:
class App extends Component {
componentDidUpdate = () => {
if (this.props.text && this.props.text.toString().trim()) {
Alert.alert(this.props.title || 'Mensagem', this.props.text.toString());
this.props.clearMessage();
}
}
render() {
return (
<NavigationContainer>
<Navigator />
</NavigationContainer>
)
}
}
const mapStateToProps = ({ message }) => {
return {
title: message.title,
text: message.text
}
}
const mapDispatchToProps = dispatch => {
return {
clearMessage: () => dispatch(setMessage({
title: '',
text: ''
}))
}
}
const connectDispatch = connect(mapStateToProps, mapDispatchToProps);
const connectApp = connectDispatch(App);
export default connectApp;
这就是我所说的。在帖子操作中的调度中。
.then(res => {
dispatch(fetchPosts());
dispatch(postCreated());
dispatch(setMessage({
title: 'Sucesso',
text: 'Nova Postagem!'
}));
});
所有其他调度都被解雇。阻止警报被触发的不是 if,因为我已经将警报放在 if 之外。
解决方案
改变这个
componentDidUpdate = () => { ... }
为了这:
componentDidUpdate(prevProps, prevState, snapshot) { ... }
请记住 componentDidUpdate 不会在第一次渲染时触发
推荐阅读
- linux - 'script' bash 命令挂断在 bash 函数中
- python - Tensorflow:GPU 加速仅在首次运行后发生
- json - 如何只保留键数组中存在的 Postgres jsonb 对象的属性?
- sql - 在 SQL 中,如何删除左侧的前 3 个字符以及特定字符后的右侧的所有字符
- java - 为什么在我只创建单个对象时,下面的代码输出 10 3 次
- node.js - 在没有安装权限的情况下使用服务器上的模块
- php - 如何在浏览器上打印 shell_exec() 错误消息?
- php - 如何从php文件中提取标题获取内容?
- visual-c++ - 如何使用 C++ 与 One Drive 交互
- clojure - 如何执行拆分字符串作为 Clojure.shell/sh 的参数