reactjs - 如何在 react-native redux 中使用 componentDidMount 调用 componentDidUpdate()?
问题描述
我在 react-native redux 中从 api 获取数据但是当我调用componentDidMount()
函数时,它会获取数据并且它一次运行一次,但是当我调用componentDidMount()
更新componentDidUpdate()
我在设置页面中更改并保存在 redux 状态的数据时,我模拟器被挂起并且componentDidUpdate()
函数在第二个循环中运行很多时间。
有什么方法可以阻止它,它应该一次运行一次?if else
健康)状况 ?
真正的调用方法是componentDidMount() with componentDidUpdate()
什么?
解决方案
componentDidMount()
仅在组件处于安装阶段时立即运行。调用componentDidMount()
insidecomponentDidUpdate()
是一种反模式,会导致无限循环,从而导致无限循环。如果您需要使用刚刚从端点获取的数据更新页面,则必须调用componentDidUpdate()
方法。
如果您需要在页面上运行另一个更新之前检查prevProps
和之间的差异。this.props
编辑:您可以参考此页面来检查您必须在反应中工作的不同生命周期。 https://reactjs.org/docs/react-component.html
推荐阅读
- environment-variables - “仅支持绝对 URL”当获取 webhooks Nextjs
- apache-zookeeper - 如何在 Apache Zookeeper ver3.6.2 中禁用纯文本端口
- python-3.x - Librosa.resample() 重新采样到低于需要的速率
- widget - 动态 google.colab.widgets.TabBar
- json - 通过数组中的 json 对象属性进行 SQLite 查询
- c - 如何使用 IMG_Load 将 sdl 中的图像加载到 c 中的表面?
- django - 如何在 Django REST 框架中运行异步 curl 请求
- javascript - I want to show external url(like 'https://google.com') in div
- javascript - javascript如何阻止空格键
- javascript - Meteor.publish 调用异步函数返回错误