Actions Must Be Plain Objects In React/redux?
Solution 1:
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));
Solution 2:
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.
Post a Comment for "Actions Must Be Plain Objects In React/redux?"