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.mzph.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(…