[WIP][lexical-react] Bug Fix: React 19 StrictMode correctness #6041
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
React 19's StrictMode re-uses the results of useMemo between renders (notes). We have code in the composers that uses useMemo to create LexicalEditor which are a mutable state nightmare if shared, causing all sorts of hard to understand bugs and makes us look pretty bad in dev modes everywhere.
useState(…)[0]
should not have this issue.We can't fix the fact that you can of course make similar mistakes in your own components. We could also add documentation to the FAQ or somewhere else that shows best practices. I'm not sure exactly what practice we would recommend if you want to do some sort of editor mutation that the cleanup function doesn't take care of (e.g. set up some initial editor state with a plugin).
Closes: #6040
Test plan
Before
See #6040 for a full example
After
Unit tests pass. Not sure what the best way to run the test suite with React 19 in CI would be?