Deprecate componentWillMount Maybe?
Problem
Let's use this thread to discuss use cases for componentWillMount and alternative solutions to those problems. Generally the solution is simply to use componentDidMount and two pass rendering if necessary. There are several problems with doing global side-effects in the "componentWill" phase. That includes starting network requests or subscribing to Flux stores etc. 1) It is confusing when used with error boundaries because currently `componentWillUnmount` can be called without `componentDidMount` ever being called. `componentWill*` is a false promise until all the children have successfully completed. Currently, this only applies when error boundaries are used but we'll probably want to revert this decision and simply not call `componentWillUnmount` here. 2) The Fiber experiment doesn't really have a good way to call `componentWillUnmount` when a new render gets aborted because a higher priority update interrupted it. Similarly, our sister project ComponentKit does reconciliation in threads where it is not safe to perform side-effects yet. 3) Callbacks from `componentWillMount` that update parent components with a `setState` is completely unsupported and lead to strange and order dependent race conditions. We already know that we want to deprecate that pattern. 4) The reconciliation order of children can easily be dependent upon if you perform global side-effects in `componentWillMount`. They're already not fully guaranteed because updates can cause unexpected reconcili
Error Output
error boundaries because currently `componentWillUnmount` can be called without `componentDidMount` ever being called. `componentWill*` is a false promise until all the children have successfully completed.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: Deprecate componentWillMount Maybe?
@yaycmyk If you could use field initializers would you use that instead? [code block] or for something more complicated: [code block]
Trust Score
6 verifications
- 1
@yaycmyk If you could use field initializers would you use that instead?
@yaycmyk If you could use field initializers would you use that instead?
textor for something more complicated: ``` js class Foo { state = this.computeInitialState(); computeInitialState() { var state = { data: null}; if (this.props.something) { state.data = this.props.somethingElse; } return state; } ... } - 2
or for something more complicated:
or for something more complicated:
Validation
Resolved in facebook/react GitHub issue #7671. Community reactions: 25 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep