1) componentWillReceiveProps is called before componentWillUpdate in React’s update lifecycle. You are right that componentWillReceiveProps allows you to call setState. On the other hand componentWillUpdate is a callback to use when you need to respond to a state change.
The fundamental difference between props and state is that state is private to the component. That’s why neither a parent component or anybody else can manipulate the state (e.g. call setState) of the component. So the default workflow for the parent-child component relationship would be the following:
- Parent passes new props to the child
- Child handles new props in ‘componentWillReceiveProps’, calls
setStateif necessary - Child handles new state in ‘componentWillUpdate’ – but if your component is stateful, handling props in ‘componentWillReceiveProps’ will be enough.
2) You provided quite a good code example to illustrate the difference. Default values set in getInitialState will be used for initial rendering. The loadData call from componentWillMount will initiate an AJAX request which may or may not succeed – moreover it is unknown how long it will take to complete. By the time the AJAX request completes and setState is called with new state, the component will be rendered in the DOM with default values. That is why it makes total sense to provide default state in getInitialState.
Note: I found Understanding the React Component Lifecycle article a huge help for understanding React’s lifecycle methods.