跳转到内容

usePrevious

返回上一次渲染时的值。适用于比较值。

import { usePrevious } from 'asterui'
function CounterExample() {
const [count, setCount] = useState(0)
const prevCount = usePrevious(count)
return (
<div>
<p>Current: {count}</p>
<p>Previous: {prevCount ?? 'N/A'}</p>
<Button onClick={() => setCount(c => c + 1)}>Increment</Button>
</div>
)
}
参数类型描述
valueT要跟踪的当前值
类型描述
T | undefined先前的值(首次渲染时为 undefined)