【发布时间】:2018-01-31 05:17:35
【问题描述】:
我尝试在我的学习 react,redux 项目中使用 axom 处理 ajax 数据,但我不知道如何调度一个动作并在组件内设置状态
在组件中会挂载
componentWillMount(){
this.props.actions.addPerson();
}
商店
import { createStore, applyMiddleware } from "redux";
import rootReducer from "../reducers";
import thunk from "redux-thunk";
export default function configureStore() {
return createStore(rootReducer, applyMiddleware(thunk));
}
在行动:
import * as types from "./action-types";
import axios from "axios";
export const addPerson = person => {
var response = [];
axios
.get(`&&&&&&&&&&&`)
.then(res => {
response = res.data;
return {
type: types.ADD_PERSON,
response
};
});
};
在减速器中
import * as types from "../actions/action-types";
export default (state = [], action) => {
console.log("action======>", action);
switch (action.type) {
case types.ADD_PERSON:
console.log("here in action", action);
return [...state, action.person];
default:
return state;
}
};
我得到的 Actions 必须是普通对象。使用自定义中间件进行异步操作。
【问题讨论】:
标签: javascript react-redux redux-thunk