Git Product home page Git Product logo

react-saga-demo's Introduction

react-saga-demo

study redux-saga

saga使用总结

  • 作用
    用于管理应用程序副作用的库,让副作用的管理更容易,执行更高效,测试更容易。
  • 监听action
    所监听的action的回调函数在action的reducer执行之后才会执行
takeEvery: 监听所有匹配的action,非阻塞式
takeLatest: 监听最近一次的匹配的action,之前启动的任务如果还没有执行完那么会被取消,非阻塞式
take: 监听一次匹配的action,阻塞式,返回值是监听到的action对象
  • 发布action
put
put({type: "xxx", payload: payload})//等价于dispatch({type: "xxx", payload: payload})
  • 方便测试异步操作的call函数
//生成一条描述信息,描述了调用哪个函数,函数的参数值是什么
call(fn, arg1, arg2...)
  • 错误的处理
    两种方式:一种是使用try/catch,当yield后的primise对象被拒绝时,catch语句块就会执行;第二种是在代码中直接处理reject状态。 示例:
//方式一
function* doSth(){
    try {
        const product = yield call(Api.fetch, "/products")
        yield put({type: "success"})
    }
    catch(error){
        yield put({type: "error"})
    }
}
//方式二
function getData(){
    return Api.fetch('/pruducts')
      .then((res) => ({res}))
      .catch((err) => ({err}))
}
function* doSth(){
    const {res, err} = yield call(getData)
    if (res) {
        yield put({type: "success"})
    }
    if (err) {
        yield put({type: "error"})   
    }
}
  • 无阻塞式任务
fork: 任务会在后台运行,不用等待任务结束,返回值是一个task对象,使用cancle可以取消该任务
使用示例:  
function* doSth(){
    while(true) {
        const {user, password} = yield take('login_request')
        const task = yield fork(authorize, user, password)
        const action = yield take(['login_error', 'login_out'])
        if (action.type == 'login_out') {
            yield cancle(task)
        }

    }

}
  • 同时执行多个任务
const [user, repos] = yield [
  call(fetch, "/user"),
  call(fetch, '/repos')
]
  • 任务竞赛
//只会等待第一个完成的任务执行完成,未完成的任务将会被取消
const {post, timeout} = yield race({
  posts: call(fetch, "/post"),
  timeout: call(delay, 1000)
})
  • yield*的用法???

react-saga-demo's People

Contributors

weieyuan avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.