Open
Description
类似于 VueUse 的 useManualRefHistory
我用已有的 useHistoryTravel 简单实现了一下
import { useHistoryTravel } from "ahooks";
import { useEffect, useMemo, useState } from "react";
const useManualHistoryTravel = <T>(initialValue: T) => {
const { value, setValue, back, forward, reset } =
useHistoryTravel<T>(initialValue);
const [state, setState] = useState<T>(initialValue);
useEffect(() => value && setState(value), [value]);
const actions = useMemo(
() => ({
commit: (newState?: T) => {
setValue(newState ?? state);
},
}),
[setValue, state]
);
return {
value: state,
setState,
back,
forward,
reset,
...actions,
} as const;
};
export default useManualHistoryTravel;