使用Typescript的巧妙React上下文技巧-不是Redux
by Bill Girten
比尔·吉尔滕(Bill Girten)
使用Typescript的巧妙React上下文技巧- 不是 Redux (Clever React context tricks using Typescript — not Redux)
by Bill Girten, Martin Maza, and Alison Stuart
由Bill Girten ,Martin Maza和Alison Stuart 撰写
TLDR; Leverage React’s Context API as a light and powerful alternative to Redux.
TLDR; 利用React的Context API作为Redux的轻便而强大的替代方案。
Let’s face it: when we first started to play with React, it was like a sugar rush. Just create a .jsx file, add a couple of dependencies, and Wham-O! — lightning-quick pages.
让我们面对现实:当我们第一次开始使用React时,就像是抢糖。 只需创建一个.jsx文件,添加几个依赖项,然后Wham-O! —快速的页面。
That’s when the excitement begins.
那就是兴奋开始的时候。
Next thing you know, you feel limitless as you engineer the presentation layer of your application at jet-speed. Then you get this crazy idea to make an AJAX call to some microservice and manage the state of the app.
接下来您会知道,在以极快的速度设计应用程序的表示层时,您会感到无穷无尽。 然后,您会得到这个疯狂的主意,以对某些微服务进行AJAX调用并管理应用程序的状态。
That’s when the pain begins…
那是痛苦开始的时候……
So you search Al Gore’s amazing Internet and find out the go-to solution to manage the app’s state is Dan Abramov’s Redux. Now you’re learning about Actions, Reducers and Stores and diving into ImmutableJS just so you can manage state. After you mapStateToProps, your React component is typically engaged in what is popularly termed “prop drilling”.
因此,您搜索Al Gore令人惊叹的Internet并找到管理该应用程序状态的最佳解决方案是Dan Abramov的Redux 。 现在,您将学习有关操作,缩减器和存储的知识,并深入了解ImmutableJS ,以便您可以管理状态。 mapStateToProps之后,您的React组件通常会参与通常称为“ 道具钻探 ”的活动。
Initially, you’re okay with passing down properties from parent to child and, at times, to grandchild. However, as the application becomes more complex you notice that sometimes you are passing some properties through the component tree that are not used by a given descendant component.
最初,您可以将属性从父级传递到子级,有时还传递给孙子。 但是,随着应用程序变得越来越复杂,您会注意到有时您会通过组件树传递某些给定后代组件未使用的属性。
Now what?!? You want to be able to manage the app’s state, but you want to do so without passing properties through the hierarchy. It’s time for some clever tricks.
怎么办?!? 您希望能够管理应用程序的状态, 但是却希望不通过层次结构传递属性。 现在是时候进行一些技巧了。
React Context API如何提供帮助 (How the React Context API helps)
Facebook released the Context API in React v16.3 as a mechanism to pass the app’s assets through a Provider to any child component listening as a Consumer. This eliminates the “prop drilling” paradigm. Imagine: at any level, a parent component could define its own state (including methods) and provide them directly to any participating consumer. Additionally, you can set state by using the methods passed in by the Context’s Provider.
Facebook的发布做出Reactv16.3作为一种机制,通过提供任何子组件监听作为一个消费者通过应用程序的资产上下文API。 这消除了“道具钻探”范式。 想象一下:在任何级别,父组件都可以定义其自己的状态(包括方法),并将其直接提供给任何参与的使用者。 此外,您可以使用上下文提供者传递的方法来设置状态。
We’ll show you how to do this in the example below.
我们将在下面的示例中向您展示如何执行此操作。
Let’s Roll!
来吧!
bgirten/clever-React-Context-tricksnew React Context experimments. Contribute to bgirten/clever-React-Context-tricks development by creating an account on…github.com
bgirten / clever-React-Context会触发 新的React Context实验。 通过在… github.com 上创建一个帐户为bgirten / clever-React-Context-tricks开发做出贡献
We begin by creating an “initial” State object which will be passed from the App component to the child components. Notice that this initialState also includes methods. This approach provides the capability of defining methods only once, so you can reuse them more easily.
我们首先创建一个“初始”状态对象,该对象将从App组件传递到子组件。 请注意,此initialState还包括方法。 此方法仅提供一次定义方法的功能,因此您可以更轻松地重用它们。
Pass the initial State into the App component and provide a Context. Every component enclosed in the MyContext.Provider tag will have the capability of consuming the context (which in this case includes the initial state of the App component).
将初始状态传递到App组件中并提供上下文。 MyContext.Provider标记中包含的每个组件都具有使用上下文的能力(在这种情况下,该上下文包括App组件的初始状态)。
Bypass the “prop drilling” from the child component to grandchild component.
从子组件到孙组件绕过“辅助钻探”。
The local method handleFetchEvent provides the capability to execute the method passed by the context (in this case, updateStats). The component’s render method fires off due to this.setState.
本地方法handleFetchEvent提供了执行上下文传递的方法的能力(在本例中为updateStats)。 由于this.setState,组件的render方法将触发。
On line 21, we consume the incoming Context.Provider, allowing us to access all those members and methods defined in the App component’s initial state.
在第21行,我们使用了传入的Context.Provider,从而允许我们访问App组件的初始状态中定义的所有那些成员和方法。
Even though methods can be passed from higher levels of the DOM tree, we can also invoke re-rendering of the DOM by simply calling the setState method directly for a given React component.
即使可以从更高级别的DOM树中传递方法,我们也可以通过直接为给定的React组件直接调用setState方法来调用DOM的重新渲染。
And here we have the loaded application. Thanks for following along — you can find more awesome content from these authors at:
在这里,我们有已加载的应用程序。 感谢您的关注-您可以在以下位置找到这些作者的更多精彩内容:
Alison’s Github, Martin’s Github, and Bill’s Github
艾莉森的Github, 马丁的Github和比尔的Github
翻译自: https://www.freecodecamp.org/news/clever-react-context-tricks-using-typescript-not-redux-7e2b9c7e5bf6/
相关文章:

vagrant 介绍,安装与使用
可以帮你统一团队成员的开发环境。如果你或者你的伙伴创建了一个Vagrantfile,那么你只需要执行vagrant up就行了,所有的软件都会安装并且配置好。团队成员可以通过相同的Vagrantfile来创建他们的开发环境,无论他们是在Linux, Mac OS X, 或者W…

HTML元素的基本特性
1,Disabled 特性: 1 //Disabled 设置元素不可用: 2 3 $(this).attr("disabled","disabled") 4 5 //移除push元素的diasble特性: 6 7 $("#push").removeAttr(disabled) 2,z-index 特性…

js base64 解码
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 解码函数 function base64_decode (input) { // 解码,配合decodeURIComponent使用var base64EncodeChars "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/";…

esl8266开发之旅_从ESL老师到越南软件开发人员的旅程
esl8266开发之旅by alberto montalesi通过阿尔贝托蒙塔莱西 从ESL老师到越南软件开发人员的旅程 (My Journey from an ESL Teacher to Software Developer in Vietnam) 介绍 (Introduction) Hi, my name is Alberto, and this is the story of how I learned to code, wrote a…

洛谷P2429 制杖题 [2017年6月计划 数论10]
P2429 制杖题 题目描述 求不大于 m 的、 质因数集与给定质数集有交集的自然数之和。 输入输出格式 输入格式:第一行二个整数 n,m。 第二行 n 个整数,表示质数集内的元素 p[i]。 输出格式:一个整数,表示答案,…

微信小程序框架封装登录,网络请求等公共模块及调用示例
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 这个公共模块封装了session的获取,和fromId发送,showToast成功和失败的弹窗。 微信小程序公共通用模块 const util require(../utils/util.js);function init() {var that th…

安装centos后无法引导启动windows7的解决方法
在电脑Windows7系统上安装Centos7,安装后找不到Windows7引导菜单。 原因:因为CentOS 7已采用新式的grub2系统,所以需要进入/boot/grub2目录后使用vi编辑grub.cfg文件。 解决方法一:修改Centos 7的Grub2引导,添加Window…

git最佳实践_Git最佳实践如何为我节省大量的返工时间
git最佳实践by Hemal Patel通过赫马尔帕特尔 Git最佳实践如何为我节省大量的返工时间 (How Git best practices saved me hours of rework) Recently I was working on the task to upgrade a certificate for a NodeJS application. This was last touched two years ago for…

商品列表选择尺寸和颜色高亮,并且把选择的数据传递到下一个页面
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 需求:商品列表选择属性,给中的属性显示高亮,并且把选择的数据记录下来传递到下一个页面。 项目下载地址:点击去下载 效果图: 选择商品的属性…

Android studio 使用心得(三)—从Eclipse迁移到Android studio
断断续续的也算是把eclipse上的代码成功迁移到android studio上来了,现在,我同事继续用eclipse,我用android studio,svn上还是之前eclipse的项目,迁移成功后,我也能happy的开发了,两不误.直接来分享我捉摸的…

代码改变世界_改变世界,一次只写一行代码
代码改变世界People like to look at changing the world as a big task. I believe changing the world can be done in little steps.人们喜欢将改变世界视为一项艰巨的任务。 我相信,改变世界可以一步步完成。 The key is identifying a problem and taking a l…

14_传智播客iOS视频教程_instancetype
12312312转载于:https://www.cnblogs.com/ZHONGZHENHUA/p/7097094.html

HDU 1011-Starship Troopers(树形背包)
题意: 有n个洞,连接像一棵树,每个包含一定数量的怪和价值,给你m个士兵,每个士兵能打20个怪,杀完一个洞的怪可得该洞的价值才可继续打相连的下面的洞(每个士兵只能打一个洞)ÿ…

微信小程序自定义组件之Picker组件
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 需求: 通过JS条件判断,满足条件就弹出Picker给用户选择一个数组里面的数据。 有些朋友可能会有疑问: 1.为什么要使用自定义的Picker组件,不是有原生的组…

kotlin ++ --_顺便说一句-探索Kotlin代表团
kotlin --by Adam Arold亚当阿罗德(Adam Arold) 顺便说一句-探索Kotlin代表团 (By the way — exploring delegation in Kotlin) Kotlin has an interesting keyword, by, which can be used for delegation. There is a lot of confusion around it, so in this article we’…

网页制作之html基础学习3-css样式表
样式:CSS(Cascading Style Sheets,层叠样式表),作用是美化HTML网页。 在样式里面用 /* */ 进行注释。 1、样式表的基本概念 1.1、样式表分类 1、内联样式表 和html联合显示,控制精确,但是可重用性差&#…

Mac OS X 下查看和设置JAVA_HOME
原文链接 : http://blog.csdn.net/done58/article/details/51138057 1, 查看Java版本 打开Mac电脑,查看JAVA版本,打开终端Terminal,通过命令行查看笔者的java版本:: [html] view plaincopy bogon:~ donny$ java -vers…

微信小程序获取用户设备的信息
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 可以获取用户的手机型号,手机操作系统,微信版本,屏幕宽高等等。 Object wx.getSystemInfoSync() wx.getSystemInfo 的同步版本 返回值 Object res 属性类型说明最…

php 命令执行crud_如何使用原始JavaScript执行CRUD操作
php 命令执行crudby Zafar Saleem通过Zafar Saleem 如何使用原始JavaScript执行CRUD操作 (How to perform CRUD operations using vanilla JavaScript) Nowadays there are a number of JavaScript frameworks around such as React, Angular, Vue and so on. They all offer …

关于手机系统信息的总结
获取IMEI号: /*** 获取IMEI号* * Description:* param param activity* param return* return String*/public static String getIMEI(Activity activity) {TelephonyManager manager (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);return manage…

pat1011. World Cup Betting (20)
1011. World Cup Betting (20) 时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueWith the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing b…

如何清空定时器
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 看代码 var aaaa; //利用slice function truncate(arr) {return arr.slice(0, -1); } Page({data: {},onShow() {console.log(yyyyyyyyyyyyyyyyyyy)clearInterval(aaaa)aaaa setInterval(function () {…

如何解决JavaScript中的根查找
介绍 (Introduction) I’ve been wanting to write about this topic for a while now. I recently had the opportunity to work on simulating the GoalSeek functionality of Excel for a web application. I found the whole purpose of GoalSeek and how it works fascina…

菜单样式1:鼠标悬停向下弹出列表
JS部分:var qcloud{};$([_t_nav]).hover(function(){var _nav $(this).attr(_t_nav);clearTimeout( qcloud[ _nav _timer ] );qcloud[ _nav _timer ] setTimeout(function(){$([_t_nav]).each(function(){$(this)[ _nav $(this).attr(_t_nav) ? addClass:remo…

JS 保持数组长度为3位并且值不重复
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 需求:保存用户搜索的3次历史记录,新的代替旧的,重复的不录入。 这里有几种情况: 1.第一次搜索,搜索的历史缓存数组为空,就直接添…

Mysql 查询 字符串 (索引和通配符)
需要查询的 Mission_Info 字段 值 CYVR-0220-1240-ZYTX-1415-1740-ZUUU-9999-9999-ZZZZ-9999-9999-ZZZZ SELECT Mission_Info FROM flightplan WHERE Mission_Info LIKE %CYVR-____-____% AND Mission_Info LIKE %-ZYTX%AND instr(Mission_Info, CYVR) <instr(Mission_In…

使用Python和NLTK的自然语言处理(NLP)教程
Natural language processing (NLP) is a branch of artificial intelligence that helps computers understand, interpret, and manipulate human language.自然语言处理(NLP)是人工智能的一个分支,可以帮助计算机理解,解释和操纵人类语言。 This vid…

微信公众号网页获取用户信息
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 效果图 html 代码: <!DOCTYPE html> <html><head><meta name"viewport" content"widthdevice-width, initial-scale1.0, user-scalableno, minimum-sc…

React Native —— App
使用 React Native 作为 app 框架,Redux 作为数据层,因此我们需要选用一些支持性的技术和工具: 开源的 Parse Server 做数据存储 - 运行在 Node.js 上。Flow 用来做 React Native 的 JavaScript 输入错误检查,防止低级的输入错误。…

数据库访问性能优化
数据库访问性能优化 转载于:https://www.cnblogs.com/daishuguang/p/4707357.html