Should I use reconcile everytime I use produce?
So when creating a store, I can use produce utility to work with draft state and this then returns the new immutable state. So when that store is update using setStore, does it update it completelty? Do I need to use reconcile?
3 Replies
const useReducer = (reducer, state) => {
const [store, setStore] = createStore(state);
const dispatch = (action) => {
state = reducer(state, action);
setStore(reconcile(state));
}
return [store, dispatch];
};
I am using this reducer functionality as I have lot's of actions to deal with
yes if your reducer returns an immutable value for the next state,
reconcile
is your best bet.
it would be nice if your reducers can just mutate the current state into the next state instead of an immutable update, then you get to avoid the reconciliation. but if not, this is perfectly fineI can use produce utility to work with draft state and this then returns the new immutable state
produce
gives you a proxy to allow you to write imperative mutations and combining with setStore
fine-grainly updates the store
so, while the posted code using reconcile
works fine and sometimes necessary especially when working with api responses
you can use produce
instead so it works like dev's last point
the answer to the question in the title is No
they don't work together