react-redux圖解_如何將React連接到Redux —圖解指南

react-redux圖解

by Princiya

由Princiya

如何將React連接到Redux —圖解指南 (How to connect React to Redux — a diagrammatic guide)

This post is aimed at people who already know React and Redux. This will aid them in better understanding how things work under the hood.

這篇文章針對的是已經了解React和Redux的人。 這將幫助他們更好地了解引擎蓋下的工作方式。

When I first got into the React universe ?, ~3 years ago, I had a very hard time understanding how Redux’s mapStateToProps and mapDispatchToProps worked and were available to the React component. Here is a diagrammatic guide to better understand how Redux’s connect works with React.

大約3年前,當我第一次進入React世界?時,我很難理解Redux的m apStateToProps和m apDispatchToPropsapDispatchToProps工作的,并且可用于React組件。 這是一個圖解指南,可以更好地了解Redux的c onnect如何與React onnect工作。

Let’s say we have a Search component.

假設我們有一個Search組件。

And a classic Redux store.

和經典的Redux商店。

Let’s populate the Redux store with Action dispatchers and the Reducer state.

讓我們用Action調度程序和Reducer狀態填充Redux存儲。

The Reducer’s defaultState looks like this. The action parameter inside the Reducer function comes from the dispatchedAction.

Reducer的defaultState如下所示。 Reducer函數內部的action參數來自調度的Action.

Let’s connect the React search component with the Redux store. The React-Redux library has official React bindings for Redux.

讓我們將React搜索組件與Redux存儲連接起來。 React-Redux庫具有Redux的官方React綁定。

It provides the connect function to connect the component to the store.

它提供了connect組件連接到商店的connect功能。

mapDispatchToProps means map the Action’s dispatch function to React component’s this.props .

mapDispatchToProps意味著將Action的dispatch功能映射到React組件的this.props

The same applies to mapStateToProps , where the Reducer’s state is mapped to React component’s this.props .

這同樣適用于mapStateToProps ,其中Reducer的狀態映射到React組件的this.props

  1. Connect React to Redux.

    將React連接到Redux。
  2. The mapStateToProps and mapDispatchToProps deals with the Redux store’s state and dispatch, respectively.

    mapStateToPropsmapDispatchToProps處理Redux存儲的statedispatch

  3. Reducer’s state and the Action’s dispatch are available via this.props to the React component.

    通過this.props ,React組件可以使用this.propsstate和Action的dispatch

Let us summarize the entire React to Redux connect workflow via a button click from the React search component.

讓我們通過單擊React搜索組件中的按鈕來總結整個React to Redux連接工作流程。

  1. Click the submit button on the React search component

    點擊submit的陣營搜索組件按鈕

  2. The click function dispatches an Action. The Action dispatch function is connected to the search component via mapDispatchToProps and is made available to this.props

    click函數調度一個Action。 Action dispatch功能通過mapDispatchToProps連接到搜索組件,并且可用于this.props

  3. (out of scope for this post) The dispatched action is responsible to fetch data and dispatch another action to update the Reducer state

    (超出此帖子的范圍)調度的操作負責fetch數據并調度另一個操作以更新Reducer狀態

  4. The Reducer state updates itself with the new search data available from Step 3.

    Reducer狀態使用第3步中可用的新搜索數據進行更新。
  5. The Reducer state is already connected to this.props in the search component via mapStateToProps

    減速狀態已經連接到this.props經由搜索組件mapStateToProps

  6. this.props has the latest search data and the view now re-renders to show the updated search results

    this.props具有最新的搜索數據,該視圖現在重新呈現以顯示更新的搜索結果

翻譯自: https://www.freecodecamp.org/news/how-to-connect-react-to-redux-a-diagrammatic-guide-d2687c14750a/

react-redux圖解

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391764.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391764.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391764.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

幾種機器學習算法的優缺點

1決策樹(Decision Trees)的優缺點 決策樹的優點: 一、 決策樹易于理解和解釋.人們在通過解釋后都有能力去理解決策樹所表達的意義。 二、 對于決策樹,數據的準備往往是簡單或者是不必要的.不需要預處理數據。…

【貪心】買賣股票的最佳時機含手續費

/** 貪心:每次選取更低的價格買入,遇到高于買入的價格就出售(此時不一定是最大收益)。* 使用buy表示買入股票的價格和手續費的和。遍歷數組,如果后面的股票價格加上手續費* 小于buy,說明有更低的買入價格更新buy。如…

本科畢設論文——基于Kinect的拖拉機防撞系統

基于Kinect的拖拉機防撞系統電子信息科學與技術專業學生 sukeysun 摘要:隨著智能車輛技術的發展,智能導航定位和實時車載監控等技術被更多的應用到日常生活照。在農業領域上,車輛自主感知道路環境并制定實時避障策略還存在不足,特…

排序算法Java代碼實現(二)—— 冒泡排序

本篇內容: 冒泡排序冒泡排序 算法思想: 冒泡排序的原理是:從左到右,相鄰元素進行比較。 每次比較一輪,就會找到序列中最大的一個或最小的一個。這個數就會從序列的最右邊冒出來。 代碼實現: /*** */ packag…

創意產品 分析_使用聯合分析來發展創意

創意產品 分析Advertising finds itself in a tenacious spot these days serving two masters: creativity and data.如今,廣告業處于一個頑強的位置,服務于兩個大師:創造力和數據。 On the one hand, it values creativity; and it’s not…

leetcode 劍指 Offer 05. 替換空格

請實現一個函數,把字符串 s 中的每個空格替換成"%20"。 示例 1: 輸入:s “We are happy.” 輸出:“We%20are%20happy.” 解題思路 一次遍歷,檢查空格,然后替換 代碼 class Solution {publ…

兩個富翁打賭_打賭您無法解決這個Google面試問題。

兩個富翁打賭by Kevin Ghadyani通過凱文加迪亞尼(Kevin Ghadyani) 打賭您無法解決這個Google面試問題。 (Bet you can’t solve this Google interview question.) 將棘手的問題分解為小塊。 (Breaking tough problems into small pieces.) I wanted to see someone else’s t…

vue.js 安裝

寫 一個小小的安裝步驟 踩坑過來的 點擊.然后安裝cnpm.再接著使用文章說明繼續安裝 # 全局安裝 vue-cli $ cnpm install --global vue-cli # 創建一個基于 webpack 模板的新項目 $ vue init webpack my-project這時候一路空格 選項.當遇到第一個讓你敲 Y/N 的時候 選擇Y …

Swift 的函數和閉包

函數的關鍵字是 func ,函數定義的格式是: func funcName(para:paraType) -> returnType{// code } 復制代碼函數的參數標簽 其中參數的那部分的詳細結構是用小括號括起來,參數名,冒號,參數類型: (number…

pandas之表格樣式

在juoyter notebook中直接通過df輸出DataFrame時&#xff0c;顯示的樣式為表格樣式&#xff0c;通過sytle可對表格的樣式做一些定制&#xff0c;類似excel的條件格式。 df pd.DataFrame(np.random.rand(5,4),columns[A,B,C,D]) s df.style print(s,type(s)) #<pandas.io.f…

多層感知機 深度神經網絡_使用深度神經網絡和合同感知損失的能源產量預測...

多層感知機 深度神經網絡in collaboration with Hsu Chung Chuan, Lin Min Htoo, and Quah Jia Yong.與許忠傳&#xff0c;林敏濤和華佳勇合作。 1. Introduction1.簡介 Since the early 1990s, several countries, mostly in the European Union and North America, had sta…

ajax跨域

//遠程的地址1.通過header頭實現ajax跨域PHP文件的代碼$origin isset($_SERVER[HTTP_ORIGIN])? $_SERVER[HTTP_ORIGIN] : ; $allow_origin array(http://www.example.com, http://www.example2.com);if(in_array($origin, $allow_origin)){ header(Access-Control-Allow-Ori…

java線程并發庫之--線程同步工具CountDownLatch用法

CountDownLatch&#xff0c;一個同步輔助類&#xff0c;在完成一組正在其他線程中執行的操作之前&#xff0c;它允許一個或多個線程一直等待。 主要方法 public CountDownLatch(int count); public void countDown(); public void await() throws InterruptedException 構造方法…

leetcode 766. 托普利茨矩陣

給你一個 m x n 的矩陣 matrix 。如果這個矩陣是托普利茨矩陣&#xff0c;返回 true &#xff1b;否則&#xff0c;返回 false 。 如果矩陣上每一條由左上到右下的對角線上的元素都相同&#xff0c;那么這個矩陣是 托普利茨矩陣 。 輸入&#xff1a;matrix [[1,2,3,4],[5,1,…

藍牙調試工具如何使用_使用此有價值的工具改進您的藍牙項目:第2部分!

藍牙調試工具如何使用This post is originally from www.jaredwolff.com. 這篇文章最初來自www.jaredwolff.com。 This is Part 2 of configuring your own Bluetooth Low Energy Service using a Nordic NRF52 series processor. If you haven’t seen Part 1 go back and ch…

gRPC快速入門記錄

為什么使用grpc 1.protocl buffer一種高效的序列化結構。 2.支持http 2.0標準化協議。 http/2 1.http/2對每個源只需創建一個持久連接&#xff0c;在這一個連接內&#xff0c;可以并行的處理多個請求和響應&#xff0c;而且做到不相互影響。 2.允許客戶端和服務端實現自己的數據…

微服務、分布式、云架構構建電子商務平臺

大型企業分布式微服務云架構服務組件 實現模塊化、微服務化、原子化、灰度發布、持續集成 分布式、微服務、云架構構建電子商務平臺 commonservice eureka Netflix事件、消息總線&#xff0c;用于在集群&#xff08;例如&#xff0c;配置變化事件&#xff09;中傳播狀態變化&am…

使用Matplotlib Numpy Pandas構想泰坦尼克號高潮

Did you know, a novel predicted the Titanic sinking 14 years previously to the actual disaster???您知道嗎&#xff0c;一本小說預言泰坦尼克號在14年前沉沒到了真正的災難中&#xff1f;&#xff1f;&#xff1f; In 1898 (14 years before the Titanic sank), Amer…

spark 架構_深入研究Spark內部和架構

spark 架構by Jayvardhan Reddy通過杰伊瓦爾丹雷迪(Jayvardhan Reddy) 深入研究Spark內部和架構 (Deep-dive into Spark internals and architecture) Apache Spark is an open-source distributed general-purpose cluster-computing framework. A spark application is a JV…

使用faker生成測試數據

需要先安裝faker模塊&#xff0c;pip install faker 導入模塊中的Faker類&#xff1a;from faker import Faker 實例化faker Faker() print(姓名相關) print(姓名:,faker.name()) print(名:,faker.first_name()) print(姓:,faker.last_name()) print(男姓名:,faker.name_male(…