Skip to content
Discussion options

You must be logged in to vote

useAtomValue is for read function, and useSetAtom is for write function. But, they aren't symmetric by nature, because the API design is to follow useState.

useAtomCallback exists for rather historical reason. Its implementation is pretty simple.

export function useAtomCallback<Result, Args extends unknown[]>(
callback: (get: Getter, set: Setter, ...arg: Args) => Result,
options?: Options
): (...args: Args) => Result {
const anAtom = useMemo(
() => atom(null, (get, set, ...args: Args) => callback(get, set, ...args)),
[callback]
)
return useSetAtom(anAtom, options)
}

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by ClementMindflow
Comment options

You must be logged in to vote
1 reply
@dai-shi
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants