Ref pattern with jotai works well but...is this a bad idea?? #283
-
I have a UI where you can place items by dragging them around on a canvas. You can zoom in and out on the canvas. I store the current canvas scale in The individual items on the canvas can be dragged around like:
Doing that means that every item renders on every scale change, even though the scale isn't used in rendering, only in callbacks. I updated to this...
...and it doesn't cause a render on all the items. But I'm using global shared state. I'm basically using the atom as a container to compute the new value (the computation is just "copy") and give me a reference to a global mutable value. Is there a jotai-preferred way of achieving this so it doesn't cause renders, but does give me the value inside a callback? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Side effect in Having a mutable source in an atom is possible for expert users. const scaleRefAtom = atom({ current: 1 }) But, the above is not very good as the ref is shared among Providers. const scaleRefAtom = atom(() => ({ current: 1 })) This is better, but still not recommended for ordinary users. A jotai-preferred way is to read atom values in function Item({x, y}: {x: number, y: number}) {
// const scale = useAtomValue(canvasScaleAtom)
const [delta, setDelta] = React.useState<CanvasPosition>({x: 0, y: 0})
const [, setDeltaWithScale] = useAtom(useMemo(
() => atom(null, (get, _set, arg) => {
const scale = get(canvasScaleAtom)
setDelta({ x: arg.x / scale, y: arg.y / scale })
}),
[]
))
//... To ease this pattern, we have |
Beta Was this translation helpful? Give feedback.
Side effect in
read
function is not recommended, just like React render function.(It may work in Legacy Mode, but in Concurrent Mode, it would behave unexpectedly.)
Having a mutable source in an atom is possible for expert users.
But, the above is not very good as the ref is shared among Providers.
This is better, but still not recommended for ordinary users.
A jotai-preferred way is to read atom values in
write
.If the final destination was an atom, it would be just easy.
If you need to setState from React.useState, it will be tricky.