#reconcile within produce

4 messages · Page 1 of 1 (latest)

dire vortex
#

Is it possible to use reconcile within the produce utility?

setState(
  produce(state => {
    state.users = reconcile([...state.users, { id: 10, name: "john" }])
  })
)
agile juniper
#

Both return an update function for the store, so in those terms no. What are you trying to accomplish?

Maybe something like:

function upsertUser(user: User) {
  const index = untrack(() => state.users.findIndex((u) => u.id === user.id));
  if (index === -1) setState('users', (users) => [...users, user]);
  else setState('users', index, reconcile(user));
}
dire vortex
#

@agile juniper Thanks. I just wanted to batch update the store without calling multiple setter functions. In my case it would just be best to call the multiple setters for the nested properties.