Reputation: 3577
componentWillMount()
called before dom is rendered. shall I call my action from here or from componentDidMount()
?
What is the difference and which one should i prefer?
Upvotes: 1
Views: 492
Reputation: 1155
The plan is to deprecate componentWillMount() in React 17, so I wouldn't use that in new code.
Upvotes: 0
Reputation: 1283
componentWillMount is called before mounting, therefore if your action changes state 'synchronously' this action will not lead to a re-rendering. This would be one of the big differences. The docs suggest you use the constructor instead of this method to initialise your comp.
actions from inside componentDidMount() will lead to re-render which might get expensive if the action changes state for an upstream component (if they are stateful) network calls should be handled at this stage typically.
Upvotes: 2