Grund
Grund

Reputation: 309

ImmutebleJS in Redux - reducer get error "state.push is not a function"

Hi i try start learn use ImmutableJS in React. But i froze. When i try use List in my reducer i have this error "state.push is not a function"

Here is mi code:

    import { List, Map} from 'immutable'
    import {FETCH_CONSTRAINTS, SET_AMOUNT_VALUE, SET_TERM_VALUE} from "../actions/actions";

export default function calculator(state = List(), action){
switch (action.type){
  case FETCH_CONSTRAINTS:
    return  state.push(Map({
       constraints: action.constraints}));
   default:
     state
  }
}

i don´t understand where is a problem

Upvotes: 1

Views: 2051

Answers (2)

Nour Sammour
Nour Sammour

Reputation: 2852

I noticed 2 issues in your code:

1- the initial state should be instance from Immutable List new List()

2- return statement in switch->default

import { List, Map} from 'immutable'
import {FETCH_CONSTRAINTS, SET_AMOUNT_VALUE, SET_TERM_VALUE} from "../actions/actions";

export default function calculator(state = new List(), action){
switch (action.type){
  case FETCH_CONSTRAINTS:
    return  state.push(Map({
       constraints: action.constraints}));
  default:
    return state;
  }
}

I Hope this help.

Upvotes: 1

Grund
Grund

Reputation: 309

Thank you, already I came with was a mistake. An example has been simplified, and yes I miss you return there. But in my code, I called other actions and they do not use immutable. All day long I had it in the eyes and I was blind. But also thanks to you I solved this.

Upvotes: 0

Related Questions