useSetup
import { useSetup } from '@xoid/react'
This export can be used for creating local state inside a React component. Its most basic usage is to create a value exactly once.
useSetup
is actually a better candidate thanReact.useMemo
to create values exactly once. According to React docs, "You may rely on useMemo as a performance optimization, not as a semantic guarantee. In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components.".useSetup
hook is based onuseRef
, therefore it's guaranteed to run the callback exactly once.
It also has a second argument to consume an outer variable as a reactive atom.
Lastly, it has a second callback argument to run things on unmount.