docs/guide/upgrade.en-US.md
Compared with the ahooks v2 version, the changes in the ahooks v3 version mainly include:
useRequestWe have released the ahooks-v2 package, you can install v2 and v3 dependencies at the same time to transition upgrades.
npm install ahooks-v2 --save
npm install ahooks --save
useRequest has been rewritten:
run and runAsync two trigger functions.options parameter supports dynamic changes.pagination, loadMore, formatResult options to avoid the overload of TypeScript, it is more convenient for encapsulating more advanced Hooks based on useRequest.UseRequestProvider, it is recommended to encapsulate advanced Hooks based on useRequest instead.pagination related options, it is recommended to use usePagination or useAntdTable to achieve paging ability.loadMore related options, it is recommended to use useInfiniteScroll to achieve unlimited loading ability.fetchKey, that is, deleted concurrent request.formatResult, initialData, and throwOnError.service no longer supports string or object.runAsync and refreshAsync, the original run no longer returns Promise.onBefore and onFinally life cycles.runAsync can return current Promise.ready behaviorHow is useRequest compatible with deleted capabilities?
ahooks v3 fully supports SSR, and related documents can be found in "React Hooks & SSR".
Hooks of DOM support dynamic target, and related documents can be found in "Hooks of DOM specification".
Inside ahooks, we have made special treatment for the functions input by the user and the functions returned, to avoid the closure problem as much as possible.
The reference address of all output functions of ahooks will not change.
const [state, setState] = React.useState();
As we all know, the setState reference address returned by React.useState will not change.
All functions returned in ahooks have the same characteristics as setState, and the reference address will not change.
const [state, { toggle }] = useToggle();
For example, the reference address of toggle function returned by useToggle is always stable.
All input functions of ahooks always use the latest one.
For the received function, ahooks will do a special process to ensure that the function called each time is always the latest.
const [state, setState] = useState();
useInterval(() => {
console.log(state);
}, 1000);
For example, in the above example, the function called by useInterval is always the latest.
Related documents can be found in "ahooks function specification".
v3 fixed some problems in strict mode. Refer to "React Hooks & strict mode"
v3 fixed some problems in react-refresh (HMR) mode. Refer to "React Hooks & react-refresh (HMR)"
useBoolean
toggle no longer accepts parameterssetuseToggle
toggle no longer accepts parameterssetuseSet
has method, use state.has insteaduseCookieState
setState(null) is no longer supported to delete cookies, please use setState(undefined) or setState() insteaduseCountDown
setTargetDate, you can dynamically change options.targetDate to achieve the same effectuseLocalStorageState / useSessionStorageState
defaultValue to Options, use options.defaultValue insteadoptions.serializer and options.deserializer to support custom sequence methoduseDynamicList
sortForm was renamed to sortListuseDrag & useDrop
useExternal
useFullscreen
useVirtualList
data parameter to the function type options.itemHeight parameteruseInViewport
useScroll
{ left?: number, top?: number } to { left: number, top: number } | undefineduseSize
{ width?: number, height?: number } to { width: number, height: number } | undefineduseKeyPress
useAntdTable
options.formatResultuseFusionTable
options.formatResultusePersistFn was renamed to useMemoizedFn
Deprecated the useControlledValue naming left over from 1.0, please use useControllableValue instead
useUrlState
useControllableValue
More other optimizations
The new version of useRequest only provides the underlying capabilities of Promise management, and more advanced capabilities can be supported by encapsulating advanced Hooks based on useRequest.
options.formatResult is deleted, and the service is expected to return the data in the final format. for example:const { data } = useRequest(async () => {
const result = await getData();
return result.data;
});
The original concurrent mode of options.fetchKey is deleted. It is expected that each request action and UI will be encapsulated as a component instead of placing all requests in the parent.
options.initialData is deleted, you can do this
const { data = initialData } = useRequest(getData);
service no longer supports string or object. It is expected to be supported by encapsulating advanced Hooks based on useReqeust. for example:const useCustomHooks = (pathname, options) => {
return useRequest(() => {
return axios(pathname);
}, options);
};