1.我们将原来的componentDidMount修改,把changeHomeData定义在mapDispatch中
2.我们在home目录下的store新建三个文件
actionCreators:
import axios from 'axios';
import * as actionTypes from './actionTypes';
export const getHomeInfo = () => {
return (dispatch) => {
axios.get('/api/home.json').then((res) => {
const result = res.data.data;
const action = {
type: actionTypes.CHANGE_HOME_DATA,
articleList: result.articleList,
recommedList: result.recommedList
}
dispatch(action);
})
}
}
actionTypes.js
export const CHANGE_HOME_DATA = 'home/CHANGE_HOME_DATA';
index.js:
import * as actionCreators from './actionCreators';
import * as actionTypes from './actionTypes';
export { actionCreators,actionTypes };
4.我们在reducer中引入actionTypes,并更改变量
然后就可以了