Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
burrack opened this issue May 21, 2017 · 2 comments
Closed

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

burrack opened this issue May 21, 2017 · 2 comments

Comments

@burrack
Copy link

burrack commented May 21, 2017

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

Thanks!

@pauldijou
Copy link
Owner

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?

@burrack
Copy link
Author

burrack commented May 21, 2017

Yes, Thank you! now I get it 👍

@burrack burrack closed this as completed May 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants