简体   繁体   English

我如何在react-native redux中使用componentDidMount调用componentDidUpdate()?

[英]How can i call componentDidUpdate() with componentDidMount in react-native redux?

I am fetching data from api in react-native redux but when i call componentDidMount() function, It fetches data and It runs once at a time, but when i call componentDidMount() with componentDidUpdate() for updating data that i have changed in settings pages and saved in redux state then my emulator gets hanged and componentDidUpdate() function runs a lot of time at a second like loop. 我从API中的反应原生终极版中获取数据,但是当我打电话componentDidMount()函数,它获取数据,并在同一时间运行一次,但是当我打电话componentDidMount()componentDidUpdate()用于更新数据,我在已经改变设置页面并保存为redux状态,然后我的仿真器被挂起, componentDidUpdate()函数在第二个类似的循环中运行大量时间。

Is there any method to stop it and it should run once at a time? 有什么方法可以停止它,它应该一次运行一次? if else condition ? if else条件?

What is the real method to call componentDidMount() with componentDidUpdate() ? componentDidMount() with componentDidUpdate()调用componentDidMount() with componentDidUpdate()的真正方法是什么?

componentDidMount() only runs at once while the component is in the mounting phase. componentDidMount()仅在组件处于安装阶段时才运行。 Calling componentDidMount() inside componentDidUpdate() is an anti pattern and would result in the infinite loop which causing you an infinite loop. 调用componentDidMount()componentDidUpdate()是一个反模式,会导致该给你造成一个无限循环的无限循环。 If you need to update the page with the data you have just fetched from the endpoint you have to call componentDidUpdate() method. 如果您需要使用刚从端点获取的数据更新页面,则必须调用componentDidUpdate()方法。

And if you need to check for the difference between the prevProps and this.props before running another update on the page. 并且如果需要在页面上运行另一个更新之前检查prevPropsthis.props之间的差异。

Edit: You can refer to this page to check different life cycles you have to work in the react. 编辑:您可以参考此页面来检查必须在React中工作的不同生命周期。 https://reactjs.org/docs/react-component.html https://reactjs.org/docs/react-component.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM