Redux sagas

Redux sagas

https://github.com/redux-saga/redux-saga

簡介: 在action要放異步動作時,一般Redux的action會錯誤,所以只能用thunk或saga的方法

而saga的概念是監聽action,然後監聽到了之後執行相對應的邏輯,使用ES6 gererator的概念

注意: 如果使用saga的put之類的用法但不在generator function內的話可能會錯誤

1.一個按鈕點擊後發出一個普通action

<div onClick={() => userLoginEnter()} />

userLoginEnter() {
    this.props.userLoginEnterAction({
      password: this.state.pass,
      groupId: this.state.gid,
      userId: this.state.name
    });  
}

2.該action與reducer

export function userLoginEnter(payload) {
  return {
    payload,
    type: USER_LOGIN_ENTER
  };
}

reducer

最後寫sagas 的function與 watcher

記得要在store.js run

Last updated

Was this helpful?