跳转到内容

Callback

参考:https://github.com/pmndrs/jotai/issues/60

useAtomCallback<Result, Args extends unknown[]>(
callback: (get: Getter, set: Setter, ...arg: Args) => Result,
options?: Options
): (...args: Args) => Result

此 Hook 用于命令式地与原子交互。 它接受一个类似于原子写入函数的回调函数, 并返回一个可获取原子值的函数。

传入 Hook 的回调必须是稳定的(应使用 useCallback 包装)。

import { useEffect, useState, useCallback } from 'react'
import { Provider, atom, useAtom } from 'jotai'
import { useAtomCallback } from 'jotai/utils'
const countAtom = atom(0)
const Counter = () => {
const [count, setCount] = useAtom(countAtom)
return (
<>
{count} <button onClick={() => setCount((c) => c + 1)}>+1</button>
</>
)
}
const Monitor = () => {
const [count, setCount] = useState(0)
const readCount = useAtomCallback(
useCallback((get) => {
const currCount = get(countAtom)
setCount(currCount)
return currCount
}, []),
)
useEffect(() => {
const timer = setInterval(async () => {
console.log(readCount())
}, 1000)
return () => {
clearInterval(timer)
}
}, [readCount])
return <div>current count: {count}</div>
}

在 StackBlitz 中打开