Reputation: 1178
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
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