In redux all reducers should have default value, since redux tree is bootstrapped with @@redix/INIT action.
To workaround that, using curried producer as reducer, we have to write condition to check for undefined value of the draft. And we can't use argument's default value (draft = {count: 0}, action), because in this case we don't know if we should return it or able to mutate proxy.
const counter = produce((draft, action) => {
if (draft === undefined) {
return {count: 0};
}
switch (action.type) {
case 'INCREMENT':
return draft.count += 1;
case 'DECREMENT':
return draft.count -= 1;
}
});
But writing such check in all producers seems a little overwhelming. What if we could specify default value as a second parameter of producer, as we do it in array.reduce method?
const counter = produce((draft, action) => {
switch (action.type) {
case 'INCREMENT':
return draft.count += 1;
case 'DECREMENT':
return draft.count -= 1;
}
}, {count: 0});
Following up #105
In redux all reducers should have default value, since redux tree is bootstrapped with
@@redix/INITaction.To workaround that, using curried producer as reducer, we have to write condition to check for undefined value of the draft. And we can't use argument's default value
(draft = {count: 0}, action), because in this case we don't know if we should return it or able to mutate proxy.But writing such check in all producers seems a little overwhelming. What if we could specify default value as a second parameter of producer, as we do it in
array.reducemethod?Following up #105