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图解

react-redux图解_如何将React连接到Redux —图解指南相关推荐

  1. react java编程_快速上手React编程 PDF 下载

    资料目录: 第1章  初积React  3 1.1  什么是React  4 1.2 React解决的问题  5 1.3  使用React的好处  6 1.3.1  简单性  6 1.3.2  速度和 ...

  2. react jest测试_如何使用React测试库和Jest开始测试React应用

    react jest测试 Testing is often seen as a tedious process. It's extra code you have to write, and in s ...

  3. 创建react应用程序_如何使用React创建一个三层应用程序

    创建react应用程序 Discover Functional JavaScript was named one of the best new Functional Programming book ...

  4. react实战课程_在使用React一年后,我学到的最重要的课程

    react实战课程 by Tomas Eglinskas 由Tomas Eglinskas 在使用React一年后,我学到的最重要的课程 (The most important lessons I'v ...

  5. react前端项目_如何使用React前端设置Ruby on Rails项目

    react前端项目 The author selected the Electronic Frontier Foundation to receive a donation as part of th ...

  6. react测试组件_如何测试React组件:完整指南

    react测试组件 When I first started learning to test my apps back in the day, I would get very frustrated ...

  7. react 服务器端渲染_服务器端渲染React应用程序的动手指南

    react 服务器端渲染 In the previous article, we described how to make a production build and deploy it to a ...

  8. react前端开发_是的,React正在接管前端开发。 问题是为什么。

    react前端开发 by Samer Buna 通过Samer Buna 是的,React正在接管前端开发. 问题是为什么. (Yes, React is taking over front-end ...

  9. react页面保留_如何在React中保留已登录的用户

    react页面保留 If you run a quick Google search for persisting a logged-in user in React (or keeping a us ...

最新文章

  1. python安装库 换源操作
  2. wofstream写中文失败,导致stream对象badbit
  3. ADO连接ACCESS数据库
  4. C语言(初遍学的一些零乱笔记)
  5. python分类算法_python数据挖掘中的分类算法有哪些?
  6. django html 模板继承,Django模板的继承
  7. IOC操作Bean管理XML方式(bean 的生命周期)
  8. 视觉SLAM笔记(39) 求解 ICP
  9. 微信小程序WXML语法介绍
  10. 何时适合进行自动化测试?(上)
  11. UVA 10391 STL容器的使用
  12. C#时间判断的循环刻度判断
  13. ext2、ext3、ext4文件系统区别
  14. mac appium环境搭建
  15. 【数据挖掘】数据挖掘总结 ( K-Means 聚类算法 | 一维数据的 K-Means 聚类 ) ★
  16. 固态硬盘怎么看出厂日期_固态硬盘使用时间怎么看,固态硬盘寿命计算方法
  17. 移动硬盘——显示盘符但打不开
  18. mysql error 1205_【MySQL运维】ERROR 1205 (HY000): Lock wait timeout exceeded的解决办法
  19. 从裁员到扩招/从自研到生态,汽车行业新年新气象
  20. J_101.Java工程Properties配置文件注释中文,会自动转换为其他编码方式问题解决

热门文章

  1. java接口的定义与实现实验报告,赶紧收藏备战金三银四!
  2. 力扣(LeetCode)292. Nim游戏 巴什博奕
  3. PHP数组 转 对象/对象 转 数组
  4. shell之引号嵌套引号大全
  5. Spring 中的 LocalSessionFactoryBean和LocalContainerEntityManagerFactoryBean
  6. net core 获取网站目录
  7. JavaScript覆盖率统计实现
  8. DRUID连接池的简单使用
  9. 关于SWT/JFace中其他常用的事件
  10. Anti-Forgery Request Recipes For ASP.NET MVC And AJAX 防伪验证,防伪请求