一、context的理解

  很多优秀的React组件都通过Context来完成自己的功能,比如react-redux的<Provider />,就是通过Context提供一个全局态的store,拖拽组件react-dnd,通过Context在组件中分发DOM的Drag和Drop事件,路由组件react-router通过Context管理路由状态等等。在React组件开发中,如果用好Context,可以让你的组件变得强大,而且灵活

  当你不想在组件树中通过逐层传递props或者state的方式来传递数据时,可以使用Context来实现跨层级的组件数据传递

  使用context可以实现跨组件传递

二、如何使用context

  如果要Context发挥作用,需要用到两种组件,一个是Context生产者(Provider),通常是一个父节点,另外是一个Context的消费者(Consumer),通常是一个或者多个子节点。所以Context的使用基于生产者消费者模式。

  对于父组件,也就是Context生产者,需要通过一个静态属性childContextTypes声明提供给子组件的Context对象的属性,并实现一个实例getChildContext方法,返回一个代表Context的对象

  1、getChildContext 根组件中声明,一个函数,返回一个对象,就是context

  2、childContextTypes 根组件中声明,指定context的结构类型,如不指定,会产生错误

  3、contextTypes 子孙组件中声明,指定要接收的context的结构类型,可以只是context的一部分结构。contextTypes 没有定义,context将是一个空对象。

  4、this.context 在子孙组件中通过此来获取上下文

三、代码演示

  目录结构
  src/app.js
  src/index.js
  src/components/one.js
  src/components/two.js
  src/components/three.js

  1、app.js

import React, { Component } from 'react';
import One from "./components/one";
import PropTypes from "prop-types";
class App extends Component { //根组件中声明,指定context的结构类型,如不指定,会产生错误
static childContextTypes = {
name:PropTypes.string,
age:PropTypes.number, }
//根组件中声明,一个函数,返回一个对象,就是context
getChildContext(){
return {
name:"zhangsan",
age:,
}
}; render() {
return (
<div>
<One/>
</div>
);
}
} export default App;

  2、one.js

import React, { Component } from 'react'
import Two from "./two";
import PropTypes from "prop-types";
export default class One extends Component {
/*
contextTypes 子孙组件中声明,指定要接收的context的结构类型,
contextTypes 没有定义,context将是一个空对象。
*/
static contextTypes = {
name:PropTypes.string,
age:PropTypes.number
}
render() {
console.log(this)
return (
<div>
<Two/>
</div>
)
}
}

  3、two.js

import React, { Component } from 'react'
import Three from "./three";
import PropTypes from "prop-types";
export default class Two extends Component {
static contextTypes = {
name:PropTypes.string,
age:PropTypes.number
}
render() {
console.log(this)
return (
<div>
<Three/>
</div>
)
}
}

  4、three.js

import React, { Component } from 'react'
import PropTypes from "prop-types";
export default class Three extends Component {
static contextTypes = {
name:PropTypes.string,
age:PropTypes.number
}
render() {
console.log(this)
return (
<div> </div>
)
}
}

  5、结果

四、总结

  1、context在如下的生命周期钩子中可以使用

    constructor(props, context)

    componentWillReceiveProps(nextProps, nextContext)

    shouldComponentUpdate(nextProps, nextState, nextContext)

    componentWillUpdate(nextProps, nextState, nextContext)

    componentDidUpdate(prevProps, prevState, prevContext)

  2、context的局限性

    1. 在组件树中,如果中间某一个组件 ShouldComponentUpdate return false 了,会阻 碍 context 的正常传值,导致子组件无法获取更新。

    2. 组件本身 extends React.PureComponent 也会阻碍 context 的更新。

【react】---context的基本使用---【巷子】的更多相关文章

  1. React context基本用法

    React的context就是一个全局变量,可以从根组件跨级别在React的组件中传递.React context的API有两个版本,React16.x之前的是老版本的context,之后的是新版本的 ...

  2. [React] Prevent Unnecessary Rerenders of Compound Components using React Context

    Due to the way that React Context Providers work, our current implementation re-renders all our comp ...

  3. React Context API

    使用React 开发程序的时候,组件中的数据共享是通过数据提升,变成父组件中的属性,然后再把属性向下传递给子组件来实现的.但当程序越来越复杂,需要共享的数据也越来越多,最后可能就把共享数据直接提升到最 ...

  4. 探索 Redux4.0 版本迭代 论基础谈展望(对比 React context)

    Redux 在几天前(2018.04.18)发布了新版本,6 commits 被合入 master.从诞生起,到如今 4.0 版本,Redux 保持了使用层面的平滑过渡.同时前不久, React 也从 ...

  5. React Hooks +React Context vs Redux

    React Hooks +React Context vs Redux https://blog.logrocket.com/use-hooks-and-context-not-react-and-r ...

  6. [译]React Context

    欢迎各位指导与讨论 : ) 前言 由于笔者英语和技术水平有限,有不足的地方恳请各位指出.我会及时修正的 O(∩_∩)O 当前React版本 15.0.1 时间 2016/4/25 正文 React一个 ...

  7. react context跨组件传递信息

    从腾讯课堂看到的一则跨组件传递数据的方法,贴代码: 使用步骤: 1.在产生参数的最顶级组建中,使用childContextTypes静态属性来定义需要放入全局参数的类型 2.在父组件中,提供状态,管理 ...

  8. [译]迁移到新的 React Context Api

    随着 React 16.3.0 的发布,context api 也有了很大的更新.我已经从旧版的 api 更新到了新版.这里就分享一下我(作者)的心得体会. 回顾 下面是一个展示如何使用旧版 api ...

  9. React Context(一):隐式传递数据

    一 Context概述 Context provides a way to pass data through the component tree without having to pass pr ...

  10. react context toggleButton demo

    //toggleButton demo: //code: //1.Appb.js: import React from 'react'; import {ThemeContext, themes} f ...

随机推荐

  1. WIN7系统有些文本乱码怎么办

    有些文本工具无法编辑中文   打开控制面板,找到语言设置 区域和语言,更改系统区域设置,改成中文,重启            

  2. Spring MVC报异常:org.springframework.web.util.NestedServletException: Request processing failed

    在使用SpringMVC绑定基本类型(如String,Integer等)参数时,应通过@RequestParam注解指定具体的参数名称,否则,当源代码在非debug模式下编译后,运行时会引发Handl ...

  3. 基于Centos搭建 Firekylin 个人网站

    系统要求: CentOS 7.2 64 位操作系统 安装 Node.js 使用 yum 命令安装 Node.js curl --silent --location https://rpm.nodeso ...

  4. C++11 正则表达式——基础知识介绍

    C++11开始支持正则表达式,使得处理文本更加简洁方便.C++11 支持六种正则表达式语法:ECMAScript, basic(POSIX Basic Regular Expressions), ex ...

  5. 译:2. RabbitMQ Java Client 之 Work Queues (工作队列)

    在上篇揭开RabbitMQ的神秘面纱一文中,我们编写了程序来发送和接收来自命名队列的消息. 本篇我们将创建一个工作队列,工作队列背后的假设是每个任务都交付给一个工作者 本篇是译文,英文原文请移步:ht ...

  6. [svc]二三层数据格式&&三层数据如何匹配路由

    网络知识拾遗 tcpip的4&7层模型 PDU数据包在不同层的不同称呼 物理层(一层)PDU指数据位(Bit). 数据链路层(二层)PDU指数据帧(Frame). 网络层(三层)PDU指数据包 ...

  7. ElasticStack系列之九 & master、data 和 client 节点

    在生产环境下,如果不修改elasticsearch节点的角色信息,在高数据量,高并发的场景下集群容易出现脑裂等问题. 默认情况下,elasticsearch 集群中每个节点都有成为主节点的资格,也都存 ...

  8. 教你一招:解决Win 10安装软件时提示:文件系统错误 (-1073740940)

    1.win+R输入 gpedit.msc 2.左边计算机配置 windows设置——安全设置——本地策略——安全选项 3.在安全选项右边选择 用户账户控制:管理员批准模式中管理员的提升权限提示的行为, ...

  9. AndroidStudio 代码(导入类)报错但可正常运行,以及解决此问题后带来的系列问题解决

    首先是应用中很多导入的类都报红色异常显示找不到此类,但运行编译正常: 第一种方法: 点击AndroidStudio菜单File -> Invalidate Caches/Restar… ,在弹出 ...

  10. (原)阅读Android-Camera2Video的demo源码和调试心得

    转载请注明出处:http://www.cnblogs.com/lihaiping/p/6142512.html   最近因为项目需要使用到camera的功能,所以针对官方的demo源码进行一番阅读,并 ...