[compiler] Add enableUseKeyedState flag and improve setState-in-render errors
Adds a new enableUseKeyedState compiler flag that changes the error message for unconditional setState calls during render.
When enableUseKeyedState is enabled, the error recommends using useKeyedState(initialState, key) to reset state when dependencies change. When disabled (the default), it links to the React docs for the manual pattern of storing previous values in state.
Both error messages now include helpful bullet points explaining the two main alternatives:
- Use useKeyedState (or manual pattern) to reset state when other state/props change
- Compute derived data directly during render without using state
useState useKeyedState
This is killing the simplicity of react
Note that useKeyedState is a user space hook at Meta. We’re just experimenting with different lint rules and how to help people implement specific patterns. It sounds like you’d prefer we make React easier to use, that’s what we’re trying to do, please let us cook.
@josephsavona Updated!