Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Lazy initializer for useState

Code

const [thing, setThing] = useState(() => { some_expensive_operation ? 'yes' : 'no' })

If you pass a function as the argument to useState() it'll use that for the initial load and only call that when necessary instead of on each render

TODO: Confirm that code snippet