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

Updating state with immer doesnt work #156

Open
yarinsa opened this issue Jan 21, 2024 · 3 comments
Open

Updating state with immer doesnt work #156

yarinsa opened this issue Jan 21, 2024 · 3 comments
Assignees
Labels
needs repro A reproduction is needed question Further information is requested

Comments

@yarinsa
Copy link

yarinsa commented Jan 21, 2024

Hey,
I am using Immer as middleware,
Ideally I wouldn't have to use 'handleSet' at all,
But assuming I have to to get access to Immer 'draft', seems like the draft is not propagated to the handle set
What approach should I do here? And maybe there is a workaround I can apply?
I tried the following setup:

export const useBoundStore = createWithEqualityFn<BoundStore>()(
  temporal(
    immer((...args) => {
      return {
        ...createGraphSlice(...args),
        ...createSelectedSlice(...args),
        ...createGlobalSlice(...args),
      };
    }),
    {
      handleSet: (handleSet) => {
        return throttle<typeof handleSet>((state: BoundStore) => {
          console.info('handleSet called');
          handleSet(draft => {
            state.nodesLookup.forEach((node, id) => {
              if (!state.nodesLookup.has(id)) {
                draft.nodesLookup.delete(id);
              }
              else {
                draft.nodesLookup.set(id, node);
              }
            })
            return draft;

          });
        }, 1000);
      },
    },
  ),
  shallow,
);

Screenshot 2024-01-21 at 18 59 46

@charkour charkour self-assigned this Jan 21, 2024
@pstrassmann
Copy link
Collaborator

pstrassmann commented Jan 21, 2024

Hey!

Just to clarify, what are you trying to achieve? Are you trying to modify your zustand store state within handleSet? If so, I'm not sure this is the appropriate place to modify zustand store state, as I believe handleSet is used for determining when zundo should add to its state history within the temporal state attached to your zustand store.

See this example of zundo with immer:
https://codesandbox.io/p/sandbox/zustand-with-zundo-and-immer-489pty?file=%2Fsrc%2FApp.tsx%3A26%2C23

@charkour charkour added the question Further information is requested label Jan 21, 2024
@yarinsa
Copy link
Author

yarinsa commented Jan 22, 2024

Hey!

Just to clarify, what are you trying to achieve? Are you trying to modify your zustand store state within handleSet? If so, I'm not sure this is the appropriate place to modify zustand store state, as I believe handleSet is used for determining when zundo should add to its state history within the temporal state attached to your zustand store.

See this example of zundo with immer: codesandbox.io/p/sandbox/zustand-with-zundo-and-immer-489pty?file=%2Fsrc%2FApp.tsx%3A26%2C23

Thanks Iv'e looked into the example and seems like I got it right in the first time :)
But... It still doesn't work, going to debug deeper and hopefully find a solution.

I am afraid that it doesn't update my "Map" objects

    nodes: [],
    edges: [],
    nodesLookup: new Map(),
    edgesLookup: new Map(),
    getNodes: () => {
      return Array.from(get().nodesLookup.values());
    },
    getEdges: () => {
      return Array.from(get().edgesLookup.values());
    },

@yarinsa yarinsa closed this as completed Jan 22, 2024
@yarinsa yarinsa reopened this Jan 22, 2024
@charkour
Copy link
Owner

@yarinsa,

Could you provide a reproduction of the error?

@charkour charkour added the needs repro A reproduction is needed label Mar 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro A reproduction is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants