






















原理:通过同步更新 useRef 来获取最新值
// util.ts
export const useRefState = (init: any = null) => {
const [state, setState] = useState(init);
const stateRef = useRef(init);
const setProxy = (newVal: any) => {
stateRef.current = newVal;
setState(newVal);
};
const getState = () => stateRef.current;
return [state, setProxy, getState];
};
使用:
import { useRefState } from "util"
const [state, setState, getState] = useRefState(0)
state // state 值,变动后更新DOM
setState // setState,变动 state
getState() // 获取最新值
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。