react-use
react-use copied to clipboard
Add a reset history method to useStateWithHistory
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:
- using useUndoable instead of this library (but it doesn't work for deeply nested objects and is an additional dependency)
- 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 https://github.com/streamich/react-use/issues/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!