Skip to content

是否考虑添加一个 useManualHistoryTravel 来手动的记录状态 #2272

Open
@Debbl

Description

@Debbl

类似于 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;

Metadata

Metadata

Assignees

Labels

featureNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions