简体   繁体   English

如何在 yield 内的回调中在 redux-saga 中“yield put”?

[英]How to "yield put" in redux-saga within a callback inside yield?

I'm having the following problem, my idea is to use CodePush within an application that has redux-saga integrated, the problem I have is that I'm using the following library (' https://github.com/geof90/react-native-code-push-saga ') which is great for this implementation我遇到以下问题,我的想法是在集成了 redux-saga 的应用程序中使用 CodePush,我遇到的问题是我正在使用以下库(' https://github.com/geof90/react -native-code-push-saga ') 非常适合此实现

 export function* init() { try { yield put(setShowLoading(true)); yield spawn(codePushSaga, { codePushStatusDidChange: (status) => { let nextText = ''; switch (status) { case codePush.SyncStatus.CHECKING_FOR_UPDATE: case codePush.SyncStatus.AWAITING_USER_ACTION: nextText = 'Checking for Updates...'; break; case codePush.SyncStatus.DOWNLOADING_PACKAGE: nextText = 'Downloading Update...'; break; case codePush.SyncStatus.INSTALLING_UPDATE: nextText = 'Installing Update...'; break; default: break; } yield put(loadingMessage(nextText)); /* This is the problem because you can't call yield put inside another saga, I have next error A 'yield' expression is only allowed in a generator body.ts(1163) */ }, codePushDownloadDidProgress: (progress) => { console.log({ progress, }); }, syncOptions: { installMode: codePush.InstallMode.IMMEDIATE, deploymentKey: codePushDeploymentKey, }, }); } catch (error) { } }

If we see the function, the codePushStatusDidChange method is a callback that returns the possible states of code push, is it possible to do a put inside another saga?如果我们看到 function,codePushStatusDidChange 方法是一个回调,它返回代码推送的可能状态,是否可以在另一个 saga 中进行放置?

My idea is for each status change, to do a kind of dispatch to update an example message Checking for Updates... when it is CHECKING_FOR_UPDATE, Downloading Update... when it is DOWNLOADING_PACKAGE, and so on.我的想法是针对每个状态更改,执行一种调度以更新示例消息 Checking for Updates... 当它是 CHECKING_FOR_UPDATE 时,正在下载更新... 当它是 DOWNLOADING_PACKAGE 时,等等。

I have been researching the documentation of redux-saga and I have found something that refers to channels, https://redux-saga.js.org/docs/advanced/Channels/ but I can't fully understand it,我一直在研究 redux-saga 的文档,我发现了一些涉及频道的内容, https://redux-saga.js.org/docs/advanced/Channels/但我不能完全理解它,

Could someone help me, I would be very grateful if you can add a code example to understand, thank you very much in advance有人可以帮助我吗,如果你能添加一个代码示例来理解,我将不胜感激,在此先感谢你

All sagas are generator functions, so you can absolutely put from inside a saga.所有的 sagas 都是生成器函数,所以你完全可以从 saga 中put The issue is that you have changed scope to an arrow function, which cannot use yield .问题是您已将 scope 更改为无法使用yield的箭头 function。 There are a couple ways to get around this.有几种方法可以解决这个问题。

  1. Rewrite the arrow function as a generator.将箭头 function 重写为生成器。
codePushStatusDidChange: function* (status) {
  1. Pass in the regular dispatch function as an argument to the action that starts this function. This solution is not great because it involves putting non-serializable values into the redux ecosystem, which should be avoided as a rule of thumb.将常规dispatch function 作为参数传递给启动此 function 的操作。此解决方案不是很好,因为它涉及将不可序列化的值放入 redux 生态系统中,根据经验应避免这样做。 It also is a bit more complicated.它也有点复杂。 If you think it's the only option for you, I can show a code example for this.如果您认为这是您唯一的选择,我可以为此展示一个代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM