Saga通道传递用于API调用的变量

Saga通道传递用于API调用的变量

本文介绍了Redux-Saga通道传递用于API调用的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要

我正在使用redux-saga-firebase并侦听Firestore中的更改,我想将变量从我的redux存储传递给我的watcher函数.我是Redux的新手,所以遇到了一些麻烦.我想我应该使用选择器,但我不是100%做到这一点.我还要打开其他解决方案!

I'm using redux-saga-firebase and to listen for changes in Firestore and I want to pass in a variable from my redux store to my watcher function. I'm new to redux so I'm having some trouble. I think I should use a selector but I'm not 100% how to do so. I'm also open any other solutions!

代码

Watcher Saga

Watcher Saga

export function* watchSquadChannel() {
  const userData = "345fgr45gdgdfdfe";
  try {
    yield put(syncSquadLoading());
    const channel = RSF.firestore.channel(
      `data/${userData}`,
      "document"
    );
    while (true) {
      const response = yield take(channel);
      const {
        _data: {
          event: { data }
        }
      } = response;
      yield put(syncSquadSuccess(data));
    }
  } catch (error) {
    yield put(syncSquadFailure(error));
  }
}

我要传递的数据

我想传递userData,以便我只能侦听以该用户ID命名的文档中的更改.userData存储在我的redux存储中的user对象下.数据结构= state.user.userData

I want to pass in userData so I can listen only for changes in the document named with that user's ID. userData is stored in my redux store under user object. data structure = state.user.userData

提前感谢您的帮助!

推荐答案

是的,您需要使用选择器.如果您的userData准备好进入商店,那么您就已经准备就绪了-您只需要使用 select 传奇中的效果将其拉出:

Yep you're correct, you need to use a selector. If your userData is ready to go in your store you're most of the way there - you just need to use the select effect in your saga to pull it out:

const userData = yield select(getUserData)

getUserData 是一个选择器函数-它以顶级Redux state 对象作为输入(也就是商店),并返回所需的信息.我不知道您的状态对象的形状,但是选择器最终看起来像这样:

That getUserData is a selector function - it takes your top level Redux state object as input (aka the store), and returns the piece of information you need. I don't know the shape of your state object, but the selector will end up looking something like this:

const getUserData = state => state.foo.bar.userData

如果您还不是一个好习惯,那么无论您是从传奇还是在连接到组件中的状态时,总是在需要进入商店时始终使用选择器是一个好习惯.选择器可以充当商店和应用程序其余部分之间的一致界面.值得使用诸如重新选择之类的帮助器库来建立选择器库.

If you're not already it's good practice to always use a selector any time you need to dip into your store, whether that's from a saga or when connecting to state in components. Your selectors can act as a consistent interface between your store and the rest of the app. It's worth using a helper library like reselect to build up your library of selectors.

这篇关于Redux-Saga通道传递用于API调用的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 02:55