FG
๐ŸŒ Web & Full-Stack

Deprecate componentWillMount Maybe?

Freshover 7 years ago
Mar 14, 20260 views
Confidence Score75%
75%

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

Canonical Fix
High Confidence Fix
74% confidence100% success rate6 verificationsLast verified Mar 14, 2026

Solution: Deprecate componentWillMount Maybe?

Low Risk

@yaycmyk If you could use field initializers would you use that instead? [code block] or for something more complicated: [code block]

74

Trust Score

6 verifications

100% success
  1. 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?

    text
    or 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. 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

Worked: 6
Partial: 2
Last verified Mar 14, 2026

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

reactjavascripttype:-discussioncomponent:-component-api