pauldijou / redux-act

An opinionated lib to create actions and reducers for Redux

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

what is this reducer.options({ payload: false }); doing ?

burrack opened this issue · comments

Sorry, but I didn't understand how to use that.
Can you please give an example ?

Thanks!

Sure. Let's say you do the following:

// This is an action creator
const increment = createAction();
// Which is actually just a function returning the action object when called
const action = increment(42);
// The action will look like
// { type: '[1]', payload: 42}

If we were doing standard redux, you would then do:

function reducer(state, action) {
  switch(action.type) {
    case '[1]': return state + action.payload;
  }
}

But using redux-act, we assume that you want to let the lib handle the type on its own so it will extract the payload and give it to you directly inside the reducer:

const reducer = createReducer({
  (state, payload) => state + payload
})

So you do not have access to the full action object, just the data inside its payload key. It should be enough in 90% use cases. If, for whatever reason, you need the full action object, you can disable the payload extraction by setting the payload option to false.

const reducer = createReducer({
  (state, action) => state + action.payload
});

reducer.options({ payload: false });

All the samples are performing the exact same operation inside the reducer. Good enough?

Yes, Thank you! now I get it 👍