reconcile within produce

Is it possible to use reconcile within the produce utility?
setState(
produce(state => {
state.users = reconcile([...state.users, { id: 10, name: "john" }])
})
)
setState(
produce(state => {
state.users = reconcile([...state.users, { id: 10, name: "john" }])
})
)
4 Replies
peerreynders
peerreynders7mo ago
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));
}
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));
}
JonathanExcelsior
JonathanExcelsiorOP7mo ago
@peerreynders 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.
peerreynders
peerreynders7mo ago
… so wrap 'em in batch?
JonathanExcelsior
JonathanExcelsiorOP7mo ago
Interesting. I could indeed use the batch utility to improve performance. Thanks for you help!

Did you find this page helpful?