使用preloadedState在createStore之后将不可变映射转换为对象

时间:2017-03-14 11:29:41

标签: javascript reactjs redux react-redux immutable.js

我的Reom,Redux和Immutable.js创建的同构应用程序出了问题。 我的reducer初始状态是不可变Map,但是当它的服务器端呈现并作为preloadedState json传递给客户端时,它会反序列化并且它的对象类型。最后在客户端的reducer中,我有对象而不是不可变的Map,不可变的方法不起作用,我得到错误。

问题是,如何以一种方式将preloadedState传递给createStore方法,在我的reducer中我会得到不可变的map而不是object?或者我错过了什么?

collection.js

import Immutable from 'immutable';

const initialState = Immutable.Map({
    isFetching: false
});

export default function collection(state = initialState, action) {
    switch (action.type) {
        case 'REQUEST_COLLECTION':
            // here i have errors on client side that update method is not defined
            return state.update('isFetching', true);
        default:
            return state;
    }
}

reducer.js

import combineReducers from 'redux/lib/combineReducers';
import collection from './reducers/collection';

export default combineReducers({
    collection,
});

index.js

import thunk from 'redux-thunk';
import createStore from 'redux/lib/createStore';
import applyMiddleware from 'redux/lib/applyMiddleware';
import compose from 'redux/lib/compose';
import reducer from './reducer';

const preloadedState = window.__PRELOADED_STATE__ || {};

const store = createStore(
    reducer,
    preloadedState,
    compose(
        applyMiddleware(thunk)
    )
);

...

0 个答案:

没有答案