1.===ahooks:useRequest===
【说明】:
一个强大的管理异步数据请求的 Hook.
【使用】:
import { useRequest } from "ahooks"
const { data, loading } = useRequest(
() => requestFun(),
{
refreshDeps: [], // refreshDeps 变化时,useRequest 会使用之前的参数重新执行 service
onSuccess: (result) => {}, // service resolve 时触发
onError: (error) => {}, // service 报错时触发
}
);
2.===ahooks:useCreation===
【说明】:
useCreation 是 useMemo 或 useRef 的替代品。因为 useMemo 不能保证被 memo 的值一定不会被重计算,而 useCreation 可以保证这一点。而相比于 useRef,你可以使用 useCreation 创建一些常量,这些常量和 useRef 创建出来的 ref 有很多使用场景上的相似,但对于复杂常量的创建,useRef 却容易出现潜在的性能隐患。
【使用】:
import { useCreation } from "ahooks"
const foo = useCreation(() => new Foo(), []);
3.===ahooks:useSetState===
【说明】:
管理 object 类型 state 的 Hooks,用法与 class 组件的 this.setState 基本一致。
【使用】:
import { useSetState } from 'ahooks';
const [state, setState] = useSetState();
setState({ hello: 'world' });
setState({ foo: 'bar' });
setState((prev) => ({ count: prev.count + 1 }));