Skip to content

Commit

Permalink
fix: clone old state to avoid pass by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
joebobmiles committed Jun 1, 2023
1 parent e50c2d7 commit 30593fb
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/patching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,19 @@ export const patchState = (oldState: any, newState: any): any =>
* @param store The Zustand API that manages the store we want to patch.
* @param newState The new state that the Zustand store should be patched to.
*/
export const patchStore = <S extends State>(
export const patchStore = <S extends unknown>(
store: StoreApi<S>,
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
newState: any
): void =>
{
// Clone the oldState instead of using it directly from store.getState().
const oldState = {
...(store.getState() as Record<string, unknown>),
};

store.setState(
patchState(store.getState() || {}, newState),
patchState(oldState, newState),
true // Replace with the patched state.
);
};

0 comments on commit 30593fb

Please sign in to comment.