Replies: 1 comment
-
Correct, you should not use hooks from outside of the render function or other hooks. So, you should not call them from an effects callback. This approach is also not optimal because it is subscribing the component to re-render for all changes in the source atom. That may be your desired behavior to keep the source and dest atoms in sync. But, if you only want a one-time move then you should not subscribe to it. Give it a whirl with a transaction: const moveAtom = useRecoilTransaction_UNSTABLE(({get, set}) => (sourceParam, destParam) => {
const value = get(myAtomFamily(sourceParam);
set(myAtomFamily(destParam), value);
}); or Recoil callback: const moveAtom = useRecoilCallback(({snapshot, set}) => async (sourceParam, destParam) => {
const value = await snapshot.getPromise(myAtomFamily(sourceParam));
set(myAtomFamily(destParam), value);
}); Adjust this depending how you want to handle pending or error states, of course. Garbage collection, when available, may release the old atom if it is no longer being referenced. |
Beta Was this translation helpful? Give feedback.
-
It seems from another issue in the tracker that the recommended way to "rename" an atom in an atom family (if the lookup param is user-editable, like a
type UniqueName = string
) is to do something likeOr if the transaction API stabilizes, wrap this in one transaction.
But it would be nice to have a feature that automated this. Basically updating the index for a specific atom in an atomFamily.
Side note: the way I've written
initNewState
above is not good I don't think. You aren't supposed to dynamically generate hooks I don't think (unless that rule only applies to useState and useEffect). So what is the way I'm supposed to create an atomFamily setter where the lookup param is dynamic over the life of the component using the hook?Should I be wrapping this in a
useRecoilCallback
somehow? Doesn't look like there is aset
available in that, so I don't think so.Beta Was this translation helpful? Give feedback.
All reactions