tim
tim

Reputation: 208

Dynamic keys using Immutable.js

I'm using Redux, but NOT React to provide a bit of context. I have a situation where I would like to store some data about pages a user has visited, but it seems like I need to set my schema prior to setting any new state in a reducer.

function reducer(state, action) {
    switch(action.type) {
        case STORE_CATEGORY:
            return Object.assign(
                {}, state,{
                    [action.category] : {
                        pages: [],
                        filters: {}
                    }
                }
            );
    }
}

The above function works, but I want to translate updating the state with an undetermined set of keys using immutable.js where the keys will be named after the category visited. Thanks.

Upvotes: 0

Views: 1220

Answers (1)

just-boris
just-boris

Reputation: 9776

As I understand, the Map from Immutable would be good for you issue

const initalState = Immutable.Map();

function reducer(state = initalState, action) {
  switch(action.type) {
    case STORE_CATEGORY:
      return state.set(action.category, {
        pages: [],
        filters: {}
      });
  }
}

Upvotes: 1

Related Questions