Skip to content

Pass default value as second parameter for curried producer #111

Description

@klimashkin

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions