您的位置:首页 > Web前端 > React

react-redux 和 redux-saga 小结

2018-07-03 09:10 609 查看
react-redux 将 store 绑定到 props 上,便于全局调用。

redux-saga 是将 redux 的同步转换为异步。

注:

dispatch 到 saga , saga 匹配行为的 type,调用接口请求数据,并将数据 通过 dispatch 传给 redux。

redux 匹配 type 并将数据绑定到 props 上

思路:

(1)routes 下的页面触发 action

this.props.dispatch({
type:"xxx", // 行为名称
payload: yyy
})


(2)saga 下的异步 redux 进行匹配

找到对应的 fork(reducer)

在 reducer 中调用整体的也是唯一的 action

yield takeLatest("xxx",(yyy) => functionName(yyy))


在 functionName 中使用 put 代替 dispatch

yield put({
type:"aaa", // 数据type
payload: bbb
})


(3)redux 下的同步 redux 进行匹配

找到对应的 reducer

在 reducer 中进行 type 匹配

case 匹配 type , return 出去的数据 需要 先

const initstate = { ccc: ddd }


初始化。

将获取的 数据 return 到 props 上

(4)全局获取 store 上的数据

const { ccc } = this.props;


.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: