首页 > 解决方案 > 如何在 react-native redux 中使用 componentDidMount 调用 componentDidUpdate()?

问题描述

我在 react-native redux 中从 api 获取数据但是当我调用componentDidMount()函数时,它会获取数据并且它一次运行一次,但是当我调用componentDidMount()更新componentDidUpdate()我在设置页面中更改并保存在 redux 状态的数据时,我模拟器被挂起并且componentDidUpdate()函数在第二个循环中运行很多时间。

有什么方法可以阻止它,它应该一次运行一次?if else健康)状况 ?

真正的调用方法是componentDidMount() with componentDidUpdate()什么?

标签: reactjsapireact-nativereduxfetch

解决方案


componentDidMount()仅在组件处于安装阶段时立即运行。调用componentDidMount()insidecomponentDidUpdate()是一种反模式,会导致无限循环,从而导致无限循环。如果您需要使用刚刚从端点获取的数据更新页面,则必须调用componentDidUpdate()方法。

如果您需要在页面上运行另一个更新之前检查prevProps和之间的差异。this.props

编辑:您可以参考此页面来检查您必须在反应中工作的不同生命周期。 https://reactjs.org/docs/react-component.html


推荐阅读