简体   繁体   中英

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.

Is there any method to stop it and it should run once at a time? if else condition ?

What is the real method to call componentDidMount() with componentDidUpdate() ?

componentDidMount() only runs at once while the component is in the mounting phase. Calling componentDidMount() inside componentDidUpdate() is an anti pattern and would result in the infinite loop which causing you an infinite loop. If you need to update the page with the data you have just fetched from the endpoint you have to call componentDidUpdate() method.

And if you need to check for the difference between the prevProps and this.props before running another update on the page.

Edit: You can refer to this page to check different life cycles you have to work in the react. https://reactjs.org/docs/react-component.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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