Actions must be plain Objects in React/Redux?

梦想的初衷 提交于 2019-12-02 10:44:37

You need to install redux-thunk to use async action. And then create your store like this

import thunk from 'redux-thunk';
const store = createStore(YOURREDUCER, applyMiddleware(thunk));

The general idea that you have above is correct. In the React component, you want to isolate the information that is relevant for the async function and call the action-creator with that information. The action creator can make an XHR call, and then dispatch actions accordingly.

(I am assuming that you have middleware installed, otherwise do look at something like redux-thunk to get started. The middleware helps connect the dots in this case, making sure async actions and results are dispatched appropriately).

Without knowing too much about the lastWeekData, lastMonthData, etc, I would only suggest possibly simplifying the 'then' part of the call. If there is any special logic, it conventionally belongs in the reducer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!