Where do I fetch initial data from server in a React Redux app?

后端 未结 2 978
情书的邮戳
情书的邮戳 2020-12-24 02:03

I\'ve started learning React / Redux and stumbled on something that is probably a very basic question. Below are snippets from my app with some code removed for simplicity s

相关标签:
2条回答
  • 2020-12-24 02:12

    There's no reason for this data load logic to touch your React components at all. What you want here is for the return of the promise to dispatch an action to your reducers, which make the appropriate changes to the store, which then causes the React components to re-render as appropriate.

    (It doesn't matter whether you kick off the async call before or after you call ReactDOM.render; the promise will work either way)

    Something like this:

    var store = createStore(publishedSitesPageReducer);
    
    someAsyncCall().then(function(response) {
      store.dispatch(someActionCreator(response));
    });
    
    ReactDOM.render(
      <Provider store={store}>
        <PublishedSitesPage />
      </Provider>,
      this.$view.find('.js-published-sites-result-target')[0]
    );
    

    Your React components are consumers of your store, but there's no rule that they need to be the ONLY consumers of your store.

    0 讨论(0)
  • 2020-12-24 02:36

    There is a clear example here on how to do it : Facebook Tutorial

    As for the endless loop, Once your array is no longer empty , Clear the Interval. This should prevent the loop.

    0 讨论(0)
提交回复
热议问题