---------------------------------  讲解一

原文:https://blog.csdn.net/xuxiaoping1989/article/details/78480758

注意: 从 React v15.5 开始 ,React.PropTypes 助手函数已被弃用,我们建议使用 prop-types 库 来定义contextTypes。
2.1首先你需要通过在终端npm install prop-types安装一个叫prop-types的第三方包

getChildContext 定义在父组件中,指定子组件可以使用的信息
childContextTypes 定义在父组件中,getChildContext 指定的传递给子组件的属性需要先通过 childContextTypes 来指定,不然会产生错误
子组件需要通过 contextTypes 指定需要访问的元素。 contextTypes 没有定义, context 将是一个空对象。

父组件定义

class Greeter extends Component{
constructor(props){
super(props)
this.state={
add:87,
remove:88
}
}
static childContextTypes = {
add:T.number,
remove:T.number
}
getChildContext() {
const { add,remove} = this.state;
return {
add,
remove
}
}
render(){
return(
<div>
<ComponetReflux/>
</div>
)
}
}

子组件定义

class ComponetReflux extends Component{
constructor(props){
super(props)
this.state={ }
}
static contextTypes = {
add:T.number,
remove:T.number
}
render(){
console.log(this.context) //打印{add:87,remove:88}
const {name,age} = this.state
return (
<div>测试context</div>
)
}
};

---------------------------------------------------------------  讲解二

原文:https://blog.csdn.net/jimolangyaleng/article/details/77715862

react推崇的是单向数据流,自上而下进行数据的传递,但是由下而上或者不在一条数据流上的组件之间的通信就会变的复杂。解决通信问题的方法很多,如果只是父子级关系,父级可以将一个回调函数当作属性传递给子级,子级可以直接调用函数从而和父级通信。

组件层级嵌套到比较深,可以使用上下文getChildContext来传递信息,这样在不需要将函数一层层往下传,任何一层的子级都可以通过this.context直接访问。

兄弟关系的组件之间无法直接通信,它们只能利用同一层的上级作为中转站。而如果兄弟组件都是最高层的组件,为了能够让它们进行通信,必须在它们外层再套一层组件,这个外层的组件起着保存数据,传递信息的作用,这其实就是redux所做的事情。

组件之间的信息还可以通过全局事件来传递。不同页面可以通过参数传递数据,下个页面可以用location.param来获取。其实react本身很简单,难的在于如何优雅高效的实现组件之间数据的交流。

今天我们就来熟悉下react的context数据传递
 

没有使用Context的情况下传递数据, 我们可以参考React的文档: Context, 它是通过组件属性一级一级往下传递. 这种方式很麻烦, 如果组件树比较深, 必须在每一个路径上的节点都引入不必要的属性.
 

定义 Context 的根组件

import React      from 'react';

# React 15.5版本以后, 使用PropTypes需要引入外部库, 直接使用React.PropTypes 会抛警告
import PropTypes from 'prop-types'; # React Router V4版本要从 react-router-dom 导入需要的组件
import { Route, Link } from 'react-router-dom'; import { Row, Col, Menu, Icon, Dropdown, Layout} from 'antd';
const { Sider, Content } = Layout; import UserList from './UserList';
import UserDetail from './UserDetail';
import Sidebar from '../_layouts/Sidebar'; const avatars = [
"elyse.png",
"kristy.png",
"matthew.png",
]; const data = [];
for(let i = 0; i <= avatars.length; i++){
data.push({key: i, name: '胡彦祖3',age: 42,address: '西湖区湖底公园1号'});
}
const columns = [
{ title: 'ID',dataIndex: 'key',key: 'key'},
{ title: '姓名',dataIndex: 'name',key: 'name', render: function(text, record, index) {
return (<Link to={`/users/${index}`}><div style={{display: 'block'}}>{text}</div></Link>)
}},
{ title: '年龄',dataIndex: 'age',key: 'age'},
{ title: '住址',dataIndex: 'address',key: 'address'},
{
title: 'Action',
key: 'action',
render: function(text, record, index){
return (
<span>
<a><Icon type="plus" /></a>
<span className="ant-divider" />
<a><Icon type="close" /></a>
</span>
)
}
}
]; class UserIndex extends React.Component {
constructor(props){
super(props)
} # 定义Context需要实现的方法 getChildContext() {
return {
data: data,
columns: columns
};
}
render(){
return (
<Layout>
<Sider>
<div id="user-side-bar" className="side-bar">
<Sidebar/>
</div>
</Sider>
<Content>
<h2 className="pagetitle">用户信息页</h2>
<Row gutter={16}>
<Col span={16}>
<UserList />
</Col>
<Col span={4}>
<Route path={`${this.props.match.url}/:id`} component={UserDetail}/>
</Col>
</Row>
</Content>
</Layout>
)
}
} # 声明Context类型 UserIndex.childContextTypes = {
data: PropTypes.array,
columns: PropTypes.array,
}; export default UserIndex;

中间组件

中间中间不再通过组件属性一级一级的往下传递了. 我们这里在 render() 函数中定义一个空的 <List/>:

import { Table, Icon } from 'antd';

import {
Link
} from 'react-router-dom'; import List from '../_common/List'; class UserList extends React.Component {
constructor(props){
super(props)
}
render(){
return (
<div>
<List />
</div>
)
}
} export default UserList;

子组件, 列表

import React from 'react';
import PropTypes from 'prop-types';
import { Layout, Table } from 'antd';
const { Sider, Content } = Layout;
class List extends React.Component {
constructor(props) {
super(props);
} render() {
return (
<Content>
<Table columns={this.context.columns} dataSource={this.context.data} size="middle" />
</Content>
);
}
} List.propTypes = {
data: PropTypes.array,
columns: PropTypes.array
}; # 在这里声明 contextTypes 用于访问 UserIndex 组件中定义的Context数据. List.contextTypes = {
data: PropTypes.array,
columns: PropTypes.array
}; export default List;

这样我们就可以在子组件中获取到父组件的数据了,不管多少层都能获取到。

React之使用Context跨组件树传递数据的更多相关文章

  1. Vue.js 父子组件相互传递数据

    父传子 : 子组件接收变量名=父组件传递的数据 如::f-cmsg="fmsg"  注意驼峰问题 子传父:@子组件关联的方法名 = 父组件接受的方法名 如:@func=" ...

  2. react context跨组件传递信息

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

  3. Vue.js 3.x 中跨层级组件如何传递数据?

    provide/inject 基本用法 在 Vue.js 中,跨层级组件如果想要传递数据,我们可以直接使用 props 来将祖先组件的数据传递给子孙组件: 注:上图来自 Vue.js 官网:Prop ...

  4. vue.js 组件之间传递数据

    前言 组件是 vue.js 最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用.如何传递数据也成了组件的重要知识点之一. 组件 组件与组件之间,还存在着不同的关 ...

  5. vue组件-构成组件-父子组件相互传递数据

    组件对于vue来说非常重要,学习学习了基础vue后,再回过头来把组件弄透! 一.概念 组件意味着协同工作,通常父子组件会是这样的关系:组件 A 在它的模版中使用了组件 B . 它们之间必然需要相互通信 ...

  6. vue 父子组件相互传递数据

    例子一 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

  7. vue组件 Prop传递数据

    组件实例的作用域是孤立的.这意味着不能(也不应该)在子组件的模板内直接引用父组件的数据.要让子组件使用父组件的数据,我们需要通过子组件的props选项. prop 是单向绑定的:当父组件的属性变化时, ...

  8. Spring 跨重定向请求传递数据

    在处理完POST请求后, 通常来讲一个最佳实践就是执行一下重定向.除了其他的一些因素外,这样做能够防止用户点击浏览器的刷新按钮或后退箭头时,客户端重新执行危险的POST请求. 在控制器方法返回的视图名 ...

  9. SpringMVC跨重定向请求传递数据

    (1)使用URL模板以路径变量和查询参数的形式传递数据(一些简单的数据) @GetMapping("/home/index") public String index(Model ...

随机推荐

  1. T-SQL 视图

    use StudentManager go --判断视图是否存在 if exists(select * from sysobjects where name='view_ScoreQuery') dr ...

  2. sas 数据集导出到excel

    PROC EXPORT DATA= Loan.BOM_FILENAME_2      OUTFILE= "D:\output.xls"      DBMS=EXCEL REPLAC ...

  3. Windows Server 2012 R2 无法启用Microsoft .NET Framework 3.5 功能

    1 在新windows 2012 R2 上安装SQL 2014 ,提示需要安装 .NET Framework 3.5 2 在添加角色和功能--功能--.NET Framework 3.5,然后失败 3 ...

  4. jdk环境变量及1.6官方下载地址

    jdk1.6: http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javas ...

  5. SSH2 No Session found for current thread原因

    Hibernate4 与 spring3 集成之后, 如果在取得session 的地方使用了getCurrentSession, 可能会报一个错:“No Session found for curre ...

  6. RxJava+Retrofit+OkHttp,一步一步封装网络框架;

    使用RxJava+Retrofit+OkHttp,首先在build.gradle添加: compile 'com.squareup.okhttp3:okhttp:3.8.1' compile 'com ...

  7. SpringBoot在Kotlin中的实现(一)

    本节记录如何用Kotlin初步搭建一个SpringBoot的环境(使用Gradle自动化构建工具). 1.新建一个Gradle的Kotlin 配置完成后,build.gradle的配置如下: buil ...

  8. NIO读写文件并加锁

    一.读取文件 package lock; import java.io.File; import java.io.FileNotFoundException; import java.io.IOExc ...

  9. 【Linux】【Tomcat】Tomcat的安装和配置等

    安装环境 :Linux(Ubuntu 版) 安装软件 : apache-tomcat-9.0.0.M1.tar.gz(下载地址http://tomcat.apache.org/) 步骤一 Tomcat ...

  10. ThinkPHP 小于5.0.24 远程代码执行高危漏洞 修复方案

    漏洞描述由于ThinkPHP5.0框架对Request类的method处理存在缺陷,导致黑客构造特定的请求,可直接GetWebShell. 漏洞评级严重 影响版本ThinkPHP 5.0系列 < ...