从 Web 服务调用加载初始数据

Loading Initial Data from a web service call

我正在阅读有关编写异步操作的教程

http://redux.js.org/docs/advanced/AsyncActions.html

基于此,我的操作中有一个名为 fetchPosts 的方法。此方法首先分派 requestPosts 操作,然后调用 Web 服务,最后分派 receivePosts 和结果。

我的问题是我应该从哪里调用此方法,以便在我的应用程序加载时从 Web 服务加载数据?

这里有一些选项取决于应用逻辑

1) 如果您想在所有组件中全局访问帖子

您可以在创建商店后立即放置 fetchPosts
示例来自 redux/examples/shopping-cart

const store = createStore(
   reducer,
   applyMiddleware(...middleware)
)

store.dispatch(getAllProducts())

2) 如果您只想访问特定 component/page

的帖子

你可以把fetchPosts调用到特定组件的componentWillMount方法中。

componentWillMount() {
    loadData()
}

部分示例来自redux官方示例

  1. redux/examples/real-world/containers/RepoPage.js
  2. redux/examples/async/containers/App.js
  3. redux/examples/real-world/containers/UserPage.js