【react】---context的基本使用---【巷子】
一、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的基本使用---【巷子】的更多相关文章
- React context基本用法
React的context就是一个全局变量,可以从根组件跨级别在React的组件中传递.React context的API有两个版本,React16.x之前的是老版本的context,之后的是新版本的 ...
- [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 ...
- React Context API
使用React 开发程序的时候,组件中的数据共享是通过数据提升,变成父组件中的属性,然后再把属性向下传递给子组件来实现的.但当程序越来越复杂,需要共享的数据也越来越多,最后可能就把共享数据直接提升到最 ...
- 探索 Redux4.0 版本迭代 论基础谈展望(对比 React context)
Redux 在几天前(2018.04.18)发布了新版本,6 commits 被合入 master.从诞生起,到如今 4.0 版本,Redux 保持了使用层面的平滑过渡.同时前不久, React 也从 ...
- React Hooks +React Context vs Redux
React Hooks +React Context vs Redux https://blog.logrocket.com/use-hooks-and-context-not-react-and-r ...
- [译]React Context
欢迎各位指导与讨论 : ) 前言 由于笔者英语和技术水平有限,有不足的地方恳请各位指出.我会及时修正的 O(∩_∩)O 当前React版本 15.0.1 时间 2016/4/25 正文 React一个 ...
- react context跨组件传递信息
从腾讯课堂看到的一则跨组件传递数据的方法,贴代码: 使用步骤: 1.在产生参数的最顶级组建中,使用childContextTypes静态属性来定义需要放入全局参数的类型 2.在父组件中,提供状态,管理 ...
- [译]迁移到新的 React Context Api
随着 React 16.3.0 的发布,context api 也有了很大的更新.我已经从旧版的 api 更新到了新版.这里就分享一下我(作者)的心得体会. 回顾 下面是一个展示如何使用旧版 api ...
- React Context(一):隐式传递数据
一 Context概述 Context provides a way to pass data through the component tree without having to pass pr ...
- react context toggleButton demo
//toggleButton demo: //code: //1.Appb.js: import React from 'react'; import {ThemeContext, themes} f ...
随机推荐
- ubuntu 安装JDK1.6(jdk-6u45-linux-x64.bin)
ubuntu 安装JDK1.6 首先在官网下载JKD1.6 linux的版本:http://www.oracle.com/technetwork/java/javasebusiness/downloa ...
- 使用SpringBoot入门案例
一.创建项目 二.给根项目UnicomCmp的pom.xml,加入parent节点(spring-boot-starter-parent) <!--Add Spring boot Parent- ...
- open jdk
http://rednaxelafx.iteye.com/blog/1549577 https://www.jianshu.com/p/f98c3acd8df8 http://download.jav ...
- 每天一个linux命令(3):ls命令
1.命令简介 ls(list 列出目录内容)命令用来列出显示指定目录里的文件及文件夹清单,缺省下ls用来打印出当前目录的清单.通过ls 命令不仅可以查看linux文件夹包含的文件,而且可以查看文件权限 ...
- 【AI】Exponential Stochastic Cellular Automata for Massively Parallel Inference - 大规模并行推理的指数随机元胞自动机
[论文标题]Exponential Stochastic Cellular Automata for Massively Parallel Inference (19th-ICAIS,PMLR ...
- java 对一个字符串进行加减乘除的运算
记录一个小程序,里面涉及到的JAVA知识点有:字符串扫描,list删除元素的方法,泛型的使用,JAVA中的/要注意的事项.有兴趣的可以看看 package com.demo; import java. ...
- 查看最新的Google地址
nslookup www.google.com 8.8.8.8
- Django Web开发学习笔记(3)
1.创建一个简单视图 这章是按照DgangoBook的说明.在我们创建的工程目录下面DjangoE_1(这是我为自己的工程命名的名字)新建一个view.py的文件,并在该文件下添加如下代码 from ...
- SessionFactory 执行原生态的SQL语句
public List<User> getPageIndex(int pageIndex,int topNum){ String hql = "select top(" ...
- 深入理解C++中public、protected及private用法
深入理解C++中public.protected及private用法 这篇文章主要介绍了C++中public.protected及private用法,对于C++面向对象程序设计来说是非常重要的概念 ...