如何在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.pswp.cn/news/393123.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/393123.shtml
英文地址,請注明出處:http://en.pswp.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 …