如何在React Native中使用Redux Saga监视网络更改

by Pritish Vaidya

通过Pritish Vaidya

如何在React Native中使用Redux Saga监视网络更改 (How to monitor network changes using Redux Saga in React Native)

为什么要使用Redux Saga监视网络更改? (Why should I use Redux Saga to monitor Network Changes?)

Redux Saga is an alternate side effect model for redux apps. It is easier to manage, execute, test and catch errors.

Redux Saga是Redux应用程序的替代 副作用模型。 它更易于管理,执行,测试和捕获错误。

Rather than implementing the logic to manage the network state in your react component, the side-effects should be handled separately. Redux Saga provides us with eventChannel as an out of the box solution for handling such cases.

与其在您的React组件中实现管理网络状态的逻辑,不如单独处理这些side-effects 。 Redux Saga为我们提供了eventChannel作为处理此类情况的现成解决方案。

什么是事件频道? (What is an Event Channel?)

Redux Saga consists of eventChannels to communicate between external events and sagas as a factory function. The events are from the event sources other than the redux store.

Redux Saga由eventChannels组成,用于在外部事件sagas之间进行通信,作为工厂功能。 这些事件来自Redux存储以外的事件源。

Here’s a basic example from the docs:

这是docs中的一个基本示例:

import { eventChannel } from 'redux-saga'function countdown(secs) {return eventChannel(emitter => {const iv = setInterval(() => {secs -= 1if (secs > 0) {emitter(secs)} else {// this causes the channel to closeemitter(END)}}, 1000);return () => {clearInterval(iv)}})
}

Things to note:

注意事项:

  • The first argument of the eventChannel is a listener function.

    eventChannel的第一个参数是侦听器函数。

  • The return method is the unregister listener function.

    返回方法是注销注册侦听器函数。

Emitter initializes the listener once after which all the events from the listener are passed to the emitter function by invoking it.

发射器初始化监听器一次,之后通过调用它将来自监听器的所有事件传递给发射器函数

我应该如何使用React Native的Network(NetInfo)API连接Redux Saga的事件频道? (How should I hook up Redux Saga’s Event Channel with React Native’s Network(NetInfo) API?)

The React Native’s NetInfo isConnected API asynchronously fetches a boolean which determines whether the device is online or offline.

React Native的NetInfo isConnected API异步获取一个布尔值 ,该布尔值确定设备是online还是offline

深入研究代码 (Dive into the code)

First, we need to create a start channel method.

首先,我们需要创建一个启动通道方法。

import { NetInfo } from "react-native"
import { eventChannel} from "redux-saga"function * startChannel() {const channel = eventChannel(listener => {const handleConnectivityChange = (isConnected) => {listener(isConnected);}// Initialise the connectivity listenerNetInfo.isConnected.addEventListener("connectionChange", handleConnectivityChange);// Return the unregister event listener.return () =>NetInfo.isConnected.removeEventListener("connectionChange",    handleConnectivityChange);});
}

The next step is to listen for the event changes within the channel.

下一步是侦听通道内的事件更改

...while (true) {const connectionInfo = yield take(channel);
}...

The final step is to pass a custom action to the channel so that the value can be synced using your action.

最后一步是将自定义操作传递到通道,以便可以使用您的操作同步值

...
function * startChannel(syncActionName) {...
while (true) {const connectionInfo = yield take(channel);yield put({type: syncActionName, status: connectionInfo });
}

This channel can be used in our default exported generator by using the call operation.

通过使用调用操作,可以在我们的默认导出生成器中使用此通道

export default function* netInfoSaga() {try {yield call(startChannel, 'CONNECTION_STATUS');} catch (e) {console.log(e);}
}

The exported generator can then be imported and used as a detached task using spawn/fork operation in our main saga.

然后,可以在我们的主要传奇中使用生成/叉操作将导出的生成器 导入并用作独立任务

用法 (Usage)

The above code has added it as a package react-native-network-status-saga to include some of the more useful and cool parameters.

上面的代码将其添加为package react-native-network-status-saga以包括一些更有用和更酷的参数。

Here are the links

这里是链接

  • GitHub

    的GitHub

  • npm

    npm

Thanks for reading. If you liked this article, show your support by clapping this article to share with other people on Medium.

谢谢阅读。 如果您喜欢这篇文章,请拍一下这篇文章以与Medium上的其他人分享以表示支持。

More of the cool stuff can be found on my StackOverflow and GitHub profiles.

在我的StackOverflow和GitHub个人资料中可以找到更多有趣的东西。

Follow me on LinkedIn, Medium, Twitter for further update new articles.

在LinkedIn , Medium和Twitter上关注我,以获取更多更新的新文章。

翻译自: https://www.freecodecamp.org/news/how-to-monitor-network-changes-using-redux-saga-in-react-native-b7b95635ef65/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/393123.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

leetcode214. 最短回文串(kmp)

给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。 示例 1: 输入: “aacecaaa” 输出: “aaacecaaa” 代码 class Solution {public int getShortestPalindrome(String s) {//求next数组的最后一…

跟我一起屏蔽百度搜索页面右侧的内容

苦恼百度搜索热点等冗杂信息很久了,然后今天下定决心解决这个问题了。 第一步:搜索,并安装插件Adblock Plus 第二步:使用拦截器 1.打开拦截器 2.具体使用 点击这一块 添加 转载于:https://www.cnblogs.com/smart-girl/p/11058774.…

JavaScript语法详解(三)

一、JavaScript循环语句 1.for循环、for/in 12345678910111213141516<!DOCTYPE html><html lang"en"> <head><meta charset"UTF-8"> <title>Title</title> </head><body><script> var array [1,2,…

鼠标拖拽吸附效果

JavaScript鼠标拖动自动吸附实例 学了几天的JavaScript&#xff0c;自己动手做了一个简单的鼠标拖动的实例&#xff0c;拖动过程中科自动检测与目标容器的距离&#xff0c;在一定的距离范围内可以自动将被拖动的元素加入到目标容器中&#xff0c;希望对开始学习javascript的童鞋…

php修改mysql数据库中的表格,如何修改mysql数据库表?

修改mysql数据库表的方法&#xff1a;使用“ALTER TABLE”语句&#xff0c;可以改变原有表的结构&#xff0c;例如增加字段或删减字段、修改原有字段数据类型、重新命名字段或表、修改表字符集等&#xff1b;语法“ALTER TABLE [修改选项]”。修改数据表的前提是数据库中已经存…

微软最新GDI漏洞MS08-052安全解决方案

微软最新GDI漏洞MS08-052安全解决方案 Simeon微软于九月九日凌晨爆出有史以来最大的安全漏洞MS08-052&#xff0c;通过该漏洞&#xff0c;攻击者可以将木马藏于图片中&#xff0c;网民无论是通过浏览器浏览、还是用各种看图软件打开、或者在即时聊天窗口、电子邮件、Office文档…

DEM轨迹后处理

方法一&#xff1a;直接在paraview中显示 首先在输出颗粒信息的时候保存global ID&#xff1a;然后在paraview中导入vtp数据&#xff08;不要导入pvd&#xff09;&#xff0c;并使用Temporal Particle To Pathlines这个filter&#xff08;可以直接ctrlspace调出搜索框搜索&…

Oracle的JDBC Url的几种方式

1.普通SID方式jdbc:oracle:thin:username/passwordx.x.x.1:1521:SID2.普通ServerName方式 jdbc:Oracle:thin:username/password//x.x.x.1:1522/ABCD3.RAC方式jdbc:oracle:thin:(DESCRIPTION(ADDRESS_LIST(ADDRESS(PROTOCOLTCP)(HOSTx.x.x.1)(PORT1521))(ADDRESS(PROTOCOLTCP)(H…

leetcode945. 使数组唯一的最小增量(排序)

给定整数数组 A&#xff0c;每次 move 操作将会选择任意 A[i]&#xff0c;并将其递增 1。 返回使 A 中的每个值都是唯一的最少操作次数。 示例 1: 输入&#xff1a;[1,2,2] 输出&#xff1a;1 解释&#xff1a;经过一次 move 操作&#xff0c;数组将变为 [1, 2, 3]。 代码 …

数据科学 python_如何使用Python为数据科学建立肌肉记忆

数据科学 pythonby Zhen Liu刘震 首先&#xff1a;数据预处理 (Up first: data preprocessing) Do you feel frustrated by breaking your data analytics flow when searching for syntax? Why do you still not remember it after looking up it for the third time?? It…

oracle 管道通信,oracle管道化表函数

转自&#xff1a;http://pengfeng.javaeye.com/blog/260360在我所做过和参与的大多数项目中,都会有用户提出的复杂的一些统计报表之内的功能要求,根据统计的复杂程度、效率及JAVA程序调用的方便性方面考虑,主要总结出以下几种方案&#xff1a; 1、SQL语句 该方案只能实现一些相…

ebtables之BROUTING和PREROUTING的redirect的区别

ebtables和iptables实用工具都使用了Netfilter框架&#xff0c;这是它们一致的一方面&#xff0c;然而对于这两者还真有一些需要联动的地方。很多人不明白ebtales的broute表的redirect和nat表PREROUTING的redirect的区别&#xff0c;其实只要记住两点即可&#xff0c;那就是对于…

LVS的四种模式的实现

LVS 是四层负载均衡&#xff0c;也就是说建立在 OSI 模型的第四层——传输层之上&#xff0c;传输层上有我们熟悉的 TCP/UDP&#xff0c;LVS 支持 TCP/UDP 的负载均衡。LVS 的转发主要通过修改 IP 地址&#xff08;NAT 模式&#xff0c;分为源地址修改 SNAT 和目标地址修改 DNA…

MyISAM与InnoDB两者之间区别与选择,详细总结,性能对比

1、MyISAM&#xff1a;默认表类型&#xff0c;它是基于传统的ISAM类型&#xff0c;ISAM是Indexed Sequential Access Method (有索引的顺序访问方法) 的缩写&#xff0c;它是存储记录和文件的标准方法。不是事务安全的&#xff0c;而且不支持外键&#xff0c;如果执行大量的sel…

leetcode557. 反转字符串中的单词 III

给定一个字符串&#xff0c;你需要反转字符串中每个单词的字符顺序&#xff0c;同时仍保留空格和单词的初始顺序。 示例&#xff1a; 输入&#xff1a;“Let’s take LeetCode contest” 输出&#xff1a;“s’teL ekat edoCteeL tsetnoc” 代码 class Solution {public St…

linux命令数据盘分多个区,pvmove命令 – 移动物理盘区

pvmove命令的作用是可以将源物理卷上的物理盘区移动到一个或多个其他的目标物理卷。使用pvmove命令时可以指定一个源日志或卷。在这种情况下&#xff0c;只有逻辑卷使用的区才会被移动到目标物理卷上的空闲或指定的区。如果没有指定的物理卷&#xff0c;则使用卷组的默认规则分…

spanning-tree extend system-id

spanning-tree extend system-id 在交换机上启用extended-system ID 特征使其支持 1024 MAC 地址, 在全局模式下使用 spanning-tree extend system-id命令.禁用时前面加 no。 spanning-tree extend system-id no spanning-tree extend system-id 命令用法 在不提供 1024 MAC 地…

leetcode841. 钥匙和房间(bfs)

有 N 个房间&#xff0c;开始时你位于 0 号房间。每个房间有不同的号码&#xff1a;0&#xff0c;1&#xff0c;2&#xff0c;…&#xff0c;N-1&#xff0c;并且房间里可能有一些钥匙能使你进入下一个房间。 在形式上&#xff0c;对于每个房间 i 都有一个钥匙列表 rooms[i]&a…

Codeforces 235C Cyclical Quest (后缀自动机)

题目链接: https://codeforces.com/contest/235/problem/C 题解: 对大串建后缀自动机 对询问串复制拆环。这里一定要注意是复制一个循环节不是复制整个串&#xff01;循环节是要整除的那种 然后要做的实际上是在大串上跑&#xff0c;每经过一个点求出当前的最长公共子串&#x…

泛型型协变逆变_Java泛型类型简介:协变和逆变

泛型型协变逆变by Fabian Terh由Fabian Terh Java泛型类型简介&#xff1a;协变和逆变 (An introduction to generic types in Java: covariance and contravariance) 种类 (Types) Java is a statically typed language, which means you must first declare a variable and …