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

can't use Map on handleActions #347

Open
vendramini opened this issue Jun 6, 2019 · 2 comments
Open

can't use Map on handleActions #347

vendramini opened this issue Jun 6, 2019 · 2 comments

Comments

@vendramini
Copy link

vendramini commented Jun 6, 2019

A simple example from docs:

const INCREMENT = 'INCREMENT';
const DECREMENT = 'DECREMENT';
const increment = createAction(INCREMENT);
const decrement = createAction(DECREMENT);

const reducer = handleActions(
    new Map([
        [
            increment,
            (state, action) => ({
                counter: state.counter + action.payload
            })
        ],

        [
            decrement,
            (state, action) => ({
                counter: state.counter - action.payload
            })
        ]
    ]),
    { counter: 0 }
);

Gives me an error:

[ error ] { Invariant Violation: Expected handlers to be a plain object.
    at invariant (/test-project/front/node_modules/invariant/invariant.js:40:15)
    at handleActions (/test-project/front/node_modules/redux-actions/lib/handleActions.js:29:26)
    at Module../redux/reducers/areas.js (/test-project/front/build/server/static/development/pages/_app.js:3385:82)
    at __webpack_require__ (/test-project/front/build/server/static/development/pages/_app.js:23:31)
    at Module../components/header/index.js (/test-project/front/build/server/static/development/pages/_app.js:464:80)
    at __webpack_require__ (/test-project/front/build/server/static/development/pages/_app.js:23:31)
    at Module../pages/_app.js (/test-project/front/build/server/static/development/pages/_app.js:3115:77)
    at __webpack_require__ (/test-project/front/build/server/static/development/pages/_app.js:23:31)
    at Object.0 (/test-project/front/build/server/static/development/pages/_app.js:3867:18)
    at __webpack_require__ (/test-project/front/build/server/static/development/pages/_app.js:23:31)
    at /test-project/front/build/server/static/development/pages/_app.js:91:18
    at Object.<anonymous> (/test-project/front/build/server/static/development/pages/_app.js:94:10)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12) name: 'Invariant Violation', framesToPop: 1 }
{ Invariant Violation: Expected handlers to be a plain object.
    at invariant (/test-project/front/node_modules/invariant/invariant.js:40:15)
    at handleActions (/test-project/front/node_modules/redux-actions/lib/handleActions.js:29:26)
    at Module../redux/reducers/areas.js (/test-project/front/build/server/static/development/pages/_app.js:3385:82)
    at __webpack_require__ (/test-project/front/build/server/static/development/pages/_app.js:23:31)
    at Module../components/header/index.js (/test-project/front/build/server/static/development/pages/_app.js:464:80)
    at __webpack_require__ (/test-project/front/build/server/static/development/pages/_app.js:23:31)
    at Module../pages/_app.js (/test-project/front/build/server/static/development/pages/_app.js:3115:77)
    at __webpack_require__ (/test-project/front/build/server/static/development/pages/_app.js:23:31)
    at Object.0 (/test-project/front/build/server/static/development/pages/_app.js:3867:18)
    at __webpack_require__ (/test-project/front/build/server/static/development/pages/_app.js:23:31)
    at /test-project/front/build/server/static/development/pages/_app.js:91:18
    at Object.<anonymous> (/test-project/front/build/server/static/development/pages/_app.js:94:10)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12) name: 'Invariant Violation', framesToPop: 1 }

I'm using Next js (react w/ server-side rendering), but I don't think it's related.

Map does exists on both client and server-side.

I changed to {type: function}, but I would like to use Map.

Any ideas?

Thanks.

@lemiesz
Copy link

lemiesz commented May 19, 2020

I have the same issue, it works if I use the type key directly

const reducer = handleActions(
    new Map([
        [
            INCREMENT,
            (state, action) => ({
                counter: state.counter + action.payload
            })
        ],

However i would like to use the action itself as a key, like how i do with handleAction

@lemiesz
Copy link

lemiesz commented May 23, 2020

I figured it out you can do

const reducer = handleActions(
    new Map([
        [
            `${INCREMENT}`,
            (state, action) => ({
                counter: state.counter + action.payload
            })
        ],

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