react icon indicating copy to clipboard operation
react copied to clipboard

[compiler] Add enableUseKeyedState flag and improve setState-in-render errors

Open poteto opened this issue 1 month ago • 2 comments

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:

  1. Use useKeyedState (or manual pattern) to reset state when other state/props change
  2. Compute derived data directly during render without using state

poteto avatar Nov 26 '25 21:11 poteto

useState useKeyedState

This is killing the simplicity of react

MrFlashAccount avatar Nov 28 '25 11:11 MrFlashAccount

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 avatar Nov 28 '25 16:11 josephsavona

@josephsavona Updated!

poteto avatar Dec 04 '25 22:12 poteto