DEV Community

Discussion on: Why you can stop writing all that Redux boilerplate

Collapse
 
richardcrng profile image
Richard Ng

Thanks for reading and responding - I'd not come across Easy Peasy, it's really interesting!

Simplifying Redux-Leaves

It's useful feedback to hear that you find Redux-Leaves complicated, as it's not intentionally so. This might be to do with how I presented it - the advanced usage is meant to be that, and simple usage covers the majority of cases.

In terms of the simple usage:

const initialState = {
  first: {
    arbitrarily: {
      nested: {
        counter: 10
      }
    }
  },
  second: {
    path: {
      to: {
        counter: 5
      }
    }
  }
}

// redux-leaves setup
const [reducer, actions] = reduxLeaves(initialState)

// vanilla redux store setup
const store = createStore(initialState)

// use redux-leaves actions
store.dispatch(actions.first.arbitrarily.nested.counter.create.increment())
store.getState().first.arbitrarily.nested.counter // => 11

store.dispatch(actions.second.path.to.counter.create.increment(100))
store.getState().second.path.to.counter // => 105

Is it the actions path that is the most complicated for you to understand?

It's meant to mimic how you'd describe the change happening:

  • at state.second.path.to.counter, you use actions.second.path.to.counter
  • then create an action, so actions.second.path.to.counter.create
  • and create an increment specifically, actions.second.path.to.counter.create.increment()

Does that make it clearer or does it still seem complicated?

Easy Peasy

It's got a similar API to Redux Toolkit, by the looks of it - which is really interesting and clearly works for a lot of people.

At a glance, it's got the same frustration I found with other approaches that I outlined - it's still premised on having to add specific cases to make updates to state, even for simple and repetitive things (like pushing to an array or incrementing a counter) - which you don't have to do with Redux-Leaves, since you: (a) get a bunch of defaults out of the box which you use at an arbitrary leaf of your state tree; and (b) if you want to use some custom logic, you can write it once and then use it anywhere in your state tree.

(With most other approaches, including Easy Peasy, you'd have to teach every child reducer [or 'model'] to do this.)

What do you think about that?