Reputation: 43
My current understanding of mapStateToProps()
is to map the currently set Redux store to any prop you want set within a component. I also know that props are variables that are passed to the component and
state` within the component is just a concurrent group of variables that are being utilized.
What is confusing me is that I am seeing on many sites/docs that you should never change the props.. Surely mapStateToProps()
is physically changing the components props and goes against this ethos?
Here's a scenario, say we are wanting to show a loading symbol while some backend process is being run, I can create a dispatch that will set IS_LOADING= True
, if I wanted to apply this value to show the loader in my component I would apply this to a prop loading in the component, however when the component first loads wouldn't it error on running mapStateToProps() as state.loading does not exist yet?
Upvotes: 2
Views: 467
Reputation: 1542
What is confusing me is that I am seeing on many sites/docs that you should never change the props
That means you should not change props from inside component. The idea is that parent creates props for the child. If props change components rerenders.
however when the component first loads wouldn't it error on running mapStateToProps() as state.loading does not exist yet?
It should not happen if you configure your store properly. Store is created on the top level, so when your child component initializes store is already present. So you have to configure your initial state properly. E. g. set initial isLoading
value to false
.
Upvotes: 1