我的初始状态的某些部分在我的应用的整个生命周期中永远不会改变。现在我想知道这种数据是否也属于商店?
如果是:
有没有办法在调用createStore()
时将这些数据置于初始状态,而没有(空)相应的reducer函数?因为数据永远不会改变,所以不需要减速器,但是combineReducers()
正在推动我有一个,否则会引发这个错误:
Unexpected key "keyName" found in initialState argument passed
to createStore. Expected to find one of the known reducer keys
instead: "otherKey1", "otherKey2". Unexpected keys will be ignored.
我正在寻找的例子:
var dataThatWillChange = function(state, action) { /* reduce */ };
var myApp = Redux.combineReducers({
dataThatWillChange: dataThatWillChange,
dataThatWillNeverChange: Redux.dummyReducer // <-- something like this?
});
var store = Redux.createStore(myApp, {
dataThatWillChange: [0, 1, 2],
dataThatWillNeverChange: { createdBy: "me" } // <-- no need for a reducer
});
答案 0 :(得分:0)
你可能只有一个总是返回初始状态的reducer,没有任何问题:
var initialState = { createdBy: "me" }
var dataThatWillNeverChange = function(state=initialState, action) {
return state;
};
var store = Redux.createStore(myApp, {
dataThatWillChange: DataThatWillChange,
dataThatWillNeverChange: dataThatWillNeverChange
});
或更紧凑:
var initialState = { createdBy: "me" }
var store = Redux.createStore(myApp, {
dataThatWillChange: DataThatWillChange,
dataThatWillNeverChange: () => initialState
});
如果您的常量数据并非真正属于&#34;应用程序状态&#34;,您还可以考虑从某个模块导出它并在需要时导入它,例如
export default {
createdBy: "me",
...
}