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

Unable to undo delete operation on array. #161

Open
indubabu opened this issue Feb 18, 2024 · 3 comments
Open

Unable to undo delete operation on array. #161

indubabu opened this issue Feb 18, 2024 · 3 comments
Labels
question Further information is requested

Comments

@indubabu
Copy link

@charkour

Adding a simple demo for reproducing the issue.

  1. Add more than one object in the array. [{id:1},{id:2},{id:3}]
  2. Remove one object. [{id:1},{id:2}]
  3. Now perform Undo

Expected result - [{id:1},{id:2},{id:3}]

Actual result -[{id:1},{id:2}]
image

@charkour
Copy link
Owner

Hi @indubabu,

Thanks for making the issue! Could you share the code that is calling undo and useStoreWithUndo? Thanks

@indubabu
Copy link
Author

Hi @charkour , Please find below for the App component.
image

@alexanderhorner
Copy link

Array.splice mutates the state directly. Use Array.slice and other immutable array methods.

removeAllBears: () => set((state) => {
  // Using slice to create a new array without the last item
  // The current array (state.bears) is not modifed
  // Instead a shallow copy is made, and new state with new array is returned
  const newArray = state.bears.slice(0, -1);
  return { bears: newArray };
}),

If you do want to use mutating methods, you should use something like immer. But i wouldn't use this unless you need to do more complex stuff like modifying a value thats deeply nested into multiple objects.

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

No branches or pull requests

3 participants