manifoldmarkets / manifold

Manifold Markets: A market for every question
https://manifold.markets
MIT License
396 stars 147 forks source link

EQUALITY IS INCOMPATIBLE WITH CAPTURING THE STATE #2665

Open sipec opened 1 month ago

sipec commented 1 month ago

react always rerenders when you call setState. so once upon a time, someone wrote a hook that checks if the thing has changed before calling setState. nevermind whether this use case is common or whether the overhead of adding a ref is worth it. I honestly don't know! there are many places we send requests to the server or db on the first render of a component, so while rerendering UI is very cheap, rerendering at all may not be? did anyone actually profile this??

anyways, to do the equality check you need to have an if(state != newState) before you call setState() which means you can't consistently do setState((capturedState) => ... correctly since the state inside the setState may differ from the state outside it, but what you really want to condition on is the inside state having changed, not the outside state.

vercel[bot] commented 1 month ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
dev ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 7, 2024 0:57am
docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 7, 2024 0:57am
prod ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 7, 2024 0:57am