## [1.安装redux-saga](https://github.com/redux-saga/redux-saga)
~~~
npm install --save redux-saga
~~~
~~~
yarn add redux-saga
~~~
## 2.配置store/index.js
~~~
import { createStore, applyMiddleware, compose } from 'redux';
import reducer from './reducer';
import createSagaMiddleware from 'redux-saga'
import mySaga from './sagas'
const sagaMiddleware = createSagaMiddleware();
const composeEnhancers =
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
}) : compose;
const enhancer = composeEnhancers(applyMiddleware(sagaMiddleware));
const store = createStore(
reducer,
enhancer
);
sagaMiddleware.run(mySaga)
export default store;
~~~
>Tip:在这个文件中配置之后,不仅reducer可以接收到action,sagas这个文件也可以接收到action。
## 3配置sagas.js
~~~
import { takeEvery, put } from 'redux-saga/effects';
import { http } from './actionTypes';
import { getHttpData } from './actionCreators';
import axios from 'axios-jsonp-pro';
function* getListData() {
try {
var url = "https://api.douban.com/v2/movie/top250"
const res = yield axios.jsonp(url);
var subjects = res.subjects;
var titles = []
subjects.forEach(ele => {
var title = ele.title
titles.push(title)
})
const action = getHttpData(titles);
yield put(action);
}catch(e){
console.log("地址无效")
}
}
function* mySaga() {
/* takeEvery捕捉action的类别 */
/* 只要接受到getHttpData,就会执行fetchUser */
/* 捕获action */
yield takeEvery(http, getListData);
}
export default mySaga;
~~~
- react
- 第一章 React入门
- 1-1 开发环境搭建
- 1-2 循环
- 1-3 jsx语法
- 1-4 react特点
- 第二章 基本语法
- 2-1 组件
- 2-2 实现一个简单的TodoList
- 2-2-1删除
- 2-3 组件之间的传值
- 2-4 子组件向父组件传值
- 2-5 react-router实现一个简单路由
- 2-6 ref的使用
- 2-7 setState方法
- 2-8 生命周期函数
- 2-9 react的css过渡动画
- 2-10 react中的内联样式
- 2-11 事件
- 2-12 箭头函数
- 第三章 redux
- 第一节 使用
- 1.1 action
- 1.2 实现todoList的增删功能
- 1.3 actionTypes的拆分
- 1.4 actionCreators.js统一管理action
- 1-5 redux设计的三大原则
- 第二节 安装Redux
- 第三节 redux进阶
- 3.1 ui组件和容器组件的拆分
- 3.2无状态组件
- 3.3 Redux-thunk中间件ajax请求数据
- 3.4redux中间件
- 3.5 redux-saga中间件
- 第四节 react-redux
- 第四章 项目启动
- 第一节 styled-components
- 1-1 style 引入背景图片
- 1-2 样式组件
- ant-design
- 1.起步