@etheryte/react-hooks
常见反应钩为无忧无忧状态和控制出流管理.
安装
yarn add @etheryte/react-hooks
钩子
useUpdateEffect(effect, dependencies?)
除初始渲染外, 对每次更新使用效果 。 接受与 React.useEffect()。 。 。 .
useAsyncEffect(asyncEffect, dependencies?)
React.useEffect() 与助动器。 效果通过函数 isStale() 中返回布尔值,表示在启动aync效应到您称之为该效应的时刻之间依赖关系是否发生变化.
useAsyncEffect(async (isStale) => {
const result = await longAsyncRequest();
// If the dependencies changed between the request start and now, discard the result
if (isStale()) {
return;
}
setFoo(result);
}, dependencies);
useAsyncUpdateEffect(asyncEffect, dependencies?)
除初始渲染外, 对每个更新使用同步效果 。 接受与 useAsyncEffect()函数 isStale() 中返回布尔值,表示在启动aync效应到您称之为该效应的时刻之间依赖关系是否发生变化.
useAsyncState(getter, dependencies?)
状态变量像 React.useState() 当依赖关系发生变化时,从同步源获取其值。 Stale 值会自动处理 .
const value = useAsyncState(async () => {
if (condition) {
return undefined;
}
return longAsyncRequest();
}, dependencies);