useSetup
import { useSetup } from '@xoid/react'
This export can be used for creating local state inside a React component. It creates a value exactly once. Its basic usage is similar to a React.useMemo
call.
useSetup
is actually more suitable 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
, thus it's guaranteed to run the callback exactly once.
When a second argument is provided, it'll be available in the callback argument as a reactive atom.
React adapter (Advanced)
Second callback argument is the React adapter. Most xoid users would not need to use this. When the second argument is not destructured, this has no runtime overhead. Its type is exported from the @xoid/react
as the following.
.effect
.effect
method simply connect to a useEffect
call internally.
You can call as many of them as you want. You can even call them conditionally. They'll connect to the same useEffect
call.
.read
.read
can be used to read the context conditionally. There's an important note about the .read
method. It's the only experimental feature of @xoid/react
and relies on React internals. This part of React MIGHT change in the future. You can consider the .read
method unsafe and not use it if your React version is -somehow- not fixed. When it's not used, it has no runtime effect. You can see the implementation here. The good thing is, it's known that react-relay
uses the same internal, and it's even supported by preact/compat
.