来自redux doc([http://redux.js.org/docs/api/store.html] [1]):
商店不是课程。这只是一个具有几种方法的对象。
,方法为:
- getState()
- 调度(动作)
- 订阅(听众)
- 替换子(nextreducer)
(在磁通中,它是相似的,也有一个ActionDisPatcher(可能是EventEmitter),但存储(S)已注册到ActionDisPatcher,因此它们是耦合的。)
)问题是为什么?为什么存储不仅是没有逻辑的数据类型,最好是不变的数据类型。
以下伪代码是显示我要说的话的示例。我使用了" AppState"单词,而不是" Store",因为它对我来说更自然:
const initialAppState = require("./initial-app-state.json");
function main() {
var actionDispatcher = new ActionDispatcher();
var appState;
actionDispatcher.register(function onAction(action) {
var newAppState = appState = reducers_combined(appState: ?AppState, action); //apply the action to appState, and create a new app state; as state is immutable
var newAppProps = createAppProps(newAppState); //we can write the createAppProps function, which takes an app state and create all the props to be passed down to the root component
ReactDom.render(React.createElement(App, newAppProps), document.getElementById("root"));
});
actionDispatcher.dispatch({
type: "LOAD_APP_REQUESTED",
appState: recordify(initialAppState); //we can write the recordify function that turns initialAppState JSONValue to an Immutable Record
})
}
如上所述,我们可以在主函数中创建我们的ActionDisPatcher单个实例,然后注册gonaction回调,该回调可以通过关闭访问当前的应用程序状态,然后将创建新的应用程序状态,更新当前的应用程序对新创建的一个人的引用,创建有关新应用状态的新应用程序道具并渲染它。视图将直接向ActionDispatcher派遣操作(ActionDisPatcher实例可以通过上下文传递到组件树或间接传递)或间接。
什么理由使商店起作用,而不是仅仅握住?有任何优点吗?
afaik,因为在创建商店时需要降低一个降量。
商店通常是用createStore(reducer, [preloadedState], [enhancer])
创建的。
您问题中提到的功能在与商店合作时有用。