- 安装redux-thunk
-
yarn add redux-thunk
-
-
在外层store文件夹下的index.js中使用
-
import { createStore , compose , applyMiddleware } from 'redux'; import thunk from 'redux-thunk'; import reducer from './reducer' const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; const store = createStore(reducer,composeEnhancers( applyMiddleware(thunk) )) export default store;
-
-
actionCreators.js中就能返回函数了
-
安装axios
-
yarn add axios
-
-
actionCreators.js中引用axios
-
import axios from 'axios';
-
-
代码
-
const changeList = (data) => ({ type:actionTypes.CHANGE_LIST, data:fromJS(data) }) export const getList = () => { return (dispatch) => { axios.get('/api/headerList.json') .then((res) => { const data = res.data; dispatch(changeList(data.data)) }) .catch(() => { console.log('catch') }) } }
-
-
模拟后台数据
-
在项目public中建立文件夹api
-
在文件夹api中创建文件headerList.json
-
就能调用headerList.json中的数据了
-
-
展现在页面上--index.js
-
{ this.props.list.map((item) => { return <SearchInfoItem key = {item}>{item}</SearchInfoItem> }) }
-
react利用redux-thunk+axios获取后台数据
最新推荐文章于 2022-12-19 10:40:20 发布