Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move state update listeners from constructor to componentDidMount #28341

Merged
merged 3 commits into from
Nov 1, 2024

Conversation

t3chguy
Copy link
Member

@t3chguy t3chguy commented Oct 30, 2024

Fixes #28306

Also ensures this.unmounted is set appropriately to account for the React 18 strict mode double-render. Without it your inline definition (private unmounted = false;) might only be called once, and you may be unmounted before you are rendered, so none of your data updates end up applying due to this.unmounted = true in the unmount before the remount.

@richvdh
Copy link
Member

richvdh commented Nov 1, 2024

I'm not really familiar with React's strict mode, so could use some help understanding the changes here. I skimmed https://react.dev/reference/react/StrictMode, but it wasn't that helpful and I could do with some help understanding its specific effects on our application.

For example, why do we need to add this.unmounted = false; to componentDidMount? This is because the same Component object can now be mounted, unmounted, and re-mounted, I guess? (Aside: I thought most of the reasons we used the this.unmounted flag no longer applied? Did you consider ripping it out rather than propagating it further?)

The PR title is "Move state update listeners from constructor to componentDidMount", but (a) the PR doesn't explain why that is a good thing to do; (b) it's not obvious that this.unmounted = false counts as a "state update listener".

A few words in the description of the PR might help understand what's going on here.

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks sane in general but some clarification would be appreciated

@t3chguy
Copy link
Member Author

t3chguy commented Nov 1, 2024

This is because the same Component object can now be mounted, unmounted, and re-mounted, I guess?

Indeed, without it your inline definition (private unmounted = false;) might only be called once, and you may be unmounted before you are rendered, so none of your data updates end up applying due to this.unmounted = true in the unmount before the remount.

Did you consider ripping it out rather than propagating it further?

I did but thought it much higher risk when in the medium term everything will be getting replaced with functional components which can deal with that.

@t3chguy t3chguy requested a review from richvdh November 1, 2024 10:55
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Base automatically changed from t3chguy/react18/createRoot.3 to develop November 1, 2024 15:33
Signed-off-by: Michael Telatynski <[email protected]>
@t3chguy t3chguy force-pushed the t3chguy/react18/createRoot.6 branch from c016f30 to 4550068 Compare November 1, 2024 16:52
Signed-off-by: Michael Telatynski <[email protected]>
@t3chguy t3chguy added this pull request to the merge queue Nov 1, 2024
Merged via the queue into develop with commit 0899165 Nov 1, 2024
32 checks passed
@t3chguy t3chguy deleted the t3chguy/react18/createRoot.6 branch November 1, 2024 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-Task Tasks for the team like planning
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants