usehooks
usehooks copied to clipboard
useHistoryState does not have previous state property
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?