Javascript 不可变映射在createStore之后转换为对象,并带有PreviedState
我用React、Redux和Immutable.js创建的同构应用程序有问题我的reducer初始状态是不可变映射,但当它在服务器端呈现并作为PreloedState json传递给客户端时,它将被反序列化,并且是对象的类型。最后,在客户端的reducer中,我有了对象而不是不可变映射,不可变方法不起作用,我会出错 问题是,如何将PrepreedState传递给createStore方法,以便在我的reducer中获得不可变的映射而不是对象?或者我错过了什么 collection.jsJavascript 不可变映射在createStore之后转换为对象,并带有PreviedState,javascript,reactjs,redux,react-redux,immutable.js,Javascript,Reactjs,Redux,React Redux,Immutable.js,我用React、Redux和Immutable.js创建的同构应用程序有问题我的reducer初始状态是不可变映射,但当它在服务器端呈现并作为PreloedState json传递给客户端时,它将被反序列化,并且是对象的类型。最后,在客户端的reducer中,我有了对象而不是不可变映射,不可变方法不起作用,我会出错 问题是,如何将PrepreedState传递给createStore方法,以便在我的reducer中获得不可变的映射而不是对象?或者我错过了什么 collection.js impo
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;
}
}
import combineReducers from 'redux/lib/combineReducers';
import collection from './reducers/collection';
export default combineReducers({
collection,
});
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)
)
);
...
reducer.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;
}
}
import combineReducers from 'redux/lib/combineReducers';
import collection from './reducers/collection';
export default combineReducers({
collection,
});
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)
)
);
...
index.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;
}
}
import combineReducers from 'redux/lib/combineReducers';
import collection from './reducers/collection';
export default combineReducers({
collection,
});
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)
)
);
...
当您使用
combineReducers
创建rootReducer时,它将返回普通对象。尝试使用combineReducers
函数的其他实现,例如,从redux immutable
包,或者编写own@disstruct谢谢,redux immutable
解决了我的问题!