React学习笔记_saga
来源:互联网 发布:非农数据8月 编辑:程序博客网 时间:2024/06/10 04:51
sagas例子
引入sagas
import createSagaMiddleware from 'redux-saga'import rootSaga from './example/saga/sagas'const sagaMiddleware = createSagaMiddleware()const store = createStore( reducer, applyMiddleware(sagaMiddleware))sagaMiddleware.run(rootSaga)
总代码:
import "babel-polyfill"import React from 'react'import ReactDOM from 'react-dom'import { createStore, applyMiddleware } from 'redux'import Counter from './example/saga/Counter'import reducer from './example/saga/reducers'import createSagaMiddleware from 'redux-saga'import rootSaga from './example/saga/sagas'const sagaMiddleware = createSagaMiddleware()const store = createStore( reducer, applyMiddleware(sagaMiddleware))sagaMiddleware.run(rootSaga)const action = type => store.dispatch({type})function render() { ReactDOM.render( <Counter value={store.getState()} onIncrement={() => action('INCREMENT')} onDecrement={() => action('DECREMENT')} onIncrementAsync={() => action('INCREMENT_ASYNC')}/>, document.getElementById('root') )}render()store.subscribe(render)
中间件代码
import {delay} from 'redux-saga'import {call, put, takeEvery, takeLatest} from 'redux-saga/effects'export function* helloSaga() { yield console.log('Hello Saga!') yield call(delay, 1000) yield console.log('Hello Saga 1000!'); yield put({type: 'INCREMENT'})}export function* incrementAsync() { yield call(delay, 1000) yield put({type: 'INCREMENT'})}export function* watchIncrementAsync() { //在任何时刻 takeLatest 只允许执行一个 fetchData 任务。并且这个任务是最后被启动的那个 //yield takeLatest('INCREMENT_ASYNC', incrementAsync) //takeEvery 允许多个 fetchData 实例同时启动。在某个特定时刻,我们可以启动一个新的 fetchData 任务, 尽管之前还有一个或多个 fetchData 尚未结束 yield takeEvery('INCREMENT_ASYNC', incrementAsync)}export function* dolog(action) { console.log('action', action)}export function* log() { yield takeEvery('*', dolog)}// single entry point to start all Sagas at onceexport default function* rootSaga() { yield [helloSaga(), watchIncrementAsync(), log() ]}
阅读全文
0 0
- React学习笔记_saga
- React学习学习笔记
- REACT学习笔记
- React.js学习笔记
- react学习笔记
- react 学习笔记1
- React学习笔记
- React-Native 学习笔记
- React学习笔记
- React学习笔记
- REACT学习笔记
- react native学习笔记
- react学习笔记
- react-redux 学习笔记
- React学习笔记
- React学习笔记(1)
- React学习笔记(2)
- React学习笔记(3)
- String字符串类实现
- leetcode解题方案--031--next-permatation
- 扩展,继承,prototype
- TCPIP相关知识
- Java四种线程池的使用
- React学习笔记_saga
- 语音识别开源项目
- bzoj 1264: [AHOI2006]基因匹配Match(树状数组)
- 遍历不规则数组的算法
- java基础(一)
- JavaScript的性能优化:加载和执行
- 功能性测试方法和流程
- 动态规划_01背包问题
- 【总结】专业英语下载历程