Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a reset history method to useStateWithHistory #2519

Open
rohaldb opened this issue Sep 4, 2023 · 0 comments
Open

Add a reset history method to useStateWithHistory #2519

rohaldb opened this issue Sep 4, 2023 · 0 comments

Comments

@rohaldb
Copy link

rohaldb commented Sep 4, 2023

Is your feature request related to a problem? Please describe.
useUndoable has a great summary of why resetting state on useStateWithHistory is useful. I set my state to the response of a network request and want to prevent the user being able to rewind history to the initial value set before the request had finished.

Describe the solution you'd like
Add a method to useStateWithHistory that follows the API of useUndoable's resetInitialState, where it takes in a param to set the state to and clears the history.

Describe alternatives you've considered
There are two alternatives I've been using:

  1. using useUndoable instead of this library (but it doesn't work for deeply nested objects and is an additional dependency)
  2. Manually extending the useStateWithHistory myself to include this method. The method is really easy to add:
  const resetInitialState = useCallback(
    (newInitialState: IHookStateInitAction<S>) => {
      innerSetState(newInitialState as S);
      history.current = [newInitialState as I];
      historyPosition.current = 0;
    },
    [],
  );

This seems to have been requested already in #1386 but I'm re-raising here because:
I believe the interface of useUndoable is relevant, I'm providing a proposed solution, and that issue has been open for over 2 years.

If approved i'd even be happy to raise a PR myself.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant