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

handleActions doesn't work #70

Closed
ntucker opened this issue Feb 23, 2016 · 2 comments
Closed

handleActions doesn't work #70

ntucker opened this issue Feb 23, 2016 · 2 comments

Comments

@ntucker
Copy link

ntucker commented Feb 23, 2016

I tried using

const INCREMENT = 'logos/counter/INCREMENT'
const RESET = 'logos/counter/RESET'
export const counterReducer = handleActions({
  INCREMENT: (state, action) => {
    return state + action.payload
  },
  RESET: (state, action) => ({
    counter: 0,
  }),
}, 0)

and the INCREMENT part was never called

however

export const counterReducer = (state=0, action) => {
  switch(action.type) {
    case INCREMENT:
      return state + action.payload
    case RESET:
      return 0
    default:
      return state
  }
}

worked just fine

@nighca
Copy link

nighca commented Feb 29, 2016

@ntucker

export const counterReducer = handleActions({
  INCREMENT: (state, action) => {
    return state + action.payload
  },
  RESET: (state, action) => ({
    counter: 0,
  }),
}, 0)

works just the same as

export const counterReducer = handleActions({
  // definition for INCREMENT and RESET do not stand for the key here
  'INCREMENT': (state, action) => {
    return state + action.payload
  },
  'RESET': (state, action) => ({
    counter: 0,
  }),
}, 0)

Maybe misunderstanding here causes your problem?

If you want to use 'logos/counter/INCREMENT'/'logos/counter/RESET' as your key (action.type), you should write like this:

export const counterReducer = handleActions({
  [INCREMENT]: (state, action) => {
    return state + action.payload
  },
  [RESET]: (state, action) => ({
    counter: 0,
  }),
}, 0)

@ntucker
Copy link
Author

ntucker commented Feb 29, 2016

Thx for that. Forgot about JS's weirdness for object declarations.

@ntucker ntucker closed this as completed Feb 29, 2016
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