Amir Hedieh
Amir Hedieh

Reputation: 1178

is it ok to fetch data in each screen mounting(navigating) React-Native

Consider an entity: Product, i have a page that shows Products to user.

in the page showing Products in componentDidMount i make API reuqest and fetch the data. now with having just a global object named GlobalState or Redux store i save the fetched data in my global state.

then in my page i use global state(as mentioned before either Redux store or just a global object) to show the data. until now this is my approach.

the question is here: each time i navigate to the page componentDidMount gets called and make the api request and populate the global state again. is this ok? if so whats the point of saving them in a global state. i think im doing sth useless again and again.

Upvotes: 0

Views: 146

Answers (1)

Elango
Elango

Reputation: 300

@Amas, your right, bcoz componentDidMount component, run each time (ie), when navigating into that screen. if you need to update the global variable value or state it's approach is okay, unless no need.

also try to change the API Request call from componentDidMount. its affecting you application running very slow. suggestion create new function paste the API Request here...

Ex:

ComponentDidMount(){ 
   this.apiReq();
} 


apiReq(){
    request code here....
 }

Upvotes: 1

Related Questions