官术网_书友最值得收藏!

The store – managing state, data retrieval, and callbacks

It's easy to think of the store as the place where our data lives. That is, however, not all it is. What the store's responsibilities are can be expressed by this list:

  • Holder of state
  • Manages the state, able to update it if need be
  • Able to handle side effects such as fetching/persisting data through HTTP
  • Handles callbacks

As you can see, that is a bit more than just storing the state. Let's now reconnect to what we were doing when we set up a listener with the dispatcher. Let's move that code into our store file, store.js, and let's persist our message content in our store:

// store.js

let store = {};

function selectIndex(index) {
store["selectedIndex"] = index;
}

dispatcher.register(message => {
switch (message.type) {
case "SELECT_INDEX":
selectIndex(message.data);
break;
}
});

OK, so now the store is being told about the new index, but an important piece is missing, how do we tell the UI? We need a way to tell the UI that something has changed. A change means that the UI should reread its data.

主站蜘蛛池模板: 大关县| 舞钢市| 霞浦县| 长武县| 郁南县| 怀集县| 建水县| 盐津县| 宁陵县| 宜黄县| 朔州市| 兴隆县| 稷山县| 隆德县| 宜兰县| 内黄县| 马尔康县| 双桥区| 平邑县| 万荣县| 重庆市| 磐安县| 电白县| 敦煌市| 广南县| 南木林县| 岳西县| 海口市| 碌曲县| 青阳县| 涡阳县| 蚌埠市| 巴东县| 南充市| 灵山县| 五指山市| 河池市| 宜宾市| 陕西省| 黄浦区| 渑池县|