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

useHistoryState does not have previous state property #331

Open
ruudandriessen opened this issue Mar 11, 2025 · 0 comments
Open

useHistoryState does not have previous state property #331

ruudandriessen opened this issue Mar 11, 2025 · 0 comments

Comments

@ruudandriessen
Copy link

ruudandriessen commented Mar 11, 2025

Currently, when we want to update the state with the useHistoryState hook, we are unable to use the same pattern useState provides, where the previous state is passed into the set function, like this:

const updateFoo = useCallback((newFoo: string) => {
   // oldValue is not available at this point
    set(oldValue => ({ 
         ... oldValue, foo: newFoo 
    });
}, [ set ])

This means we have to do this:

const updateFoo = useCallback((newFoo: string) => {
    set(({ 
         ... state, foo: newFoo 
    });
}, [ set, state ])

However, if we now have two functions to update the state, i.e.: we also have a updateBar which are called in the same onChange handler, like such:

onChange={(newValue) => {
   updateFoo(newValue.foo);
   updateBar(newValue.bar);
}}

Only the updateBar change is reflected, since we don't have the updateFoo change yet in this callback. Can we ensure we also expose the previousValue in the set handler as well?

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