feat: add useRefState hooks
处理闭包问题,在useState的基础上多了一个获取最新state的函数
To deal with the closure problem, there is an additional function to get the latest state on the basis of useState
Could u provide some usage of this hook?
@kongmoumou Of course, this hook mainly solves the problem of providing a channel inside the function closure to get the latest state value. Kind of like a combination of useState and useLatest。But his update time is earlier than useLatest, can guarantee the latest value。
demo:
import React, { useEffect } from 'react';
import { useStateRef } from 'ahooks';
export default () => {
const [value, setValue, getValue] = useStateRef(false);
useEffect(() => {
setTimeout(() => {
setValue('data loaded from server');
}, 5000);
setTimeout(() => {
console.log(getValue());
}, 6000);
}, []);
const text = value || 'Loading...';
return <div>{text}</div>;
};
https://github.com/streamich/react-use/blob/master/docs/useGetSet.md
相关 issue:https://github.com/alibaba/hooks/issues/2266
#2266 对应的 PR 会有 breaking changes。建议废弃掉 useGetState,引入这个 hook
mark 一下
好的,我完善一下
@liuyib 相关调整已同步