React之使用Context跨组件树传递数据
--------------------------------- 讲解一
原文: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 的根组件
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跨组件树传递数据的更多相关文章
- Vue.js 父子组件相互传递数据
父传子 : 子组件接收变量名=父组件传递的数据 如::f-cmsg="fmsg" 注意驼峰问题 子传父:@子组件关联的方法名 = 父组件接受的方法名 如:@func=" ...
- react context跨组件传递信息
从腾讯课堂看到的一则跨组件传递数据的方法,贴代码: 使用步骤: 1.在产生参数的最顶级组建中,使用childContextTypes静态属性来定义需要放入全局参数的类型 2.在父组件中,提供状态,管理 ...
- Vue.js 3.x 中跨层级组件如何传递数据?
provide/inject 基本用法 在 Vue.js 中,跨层级组件如果想要传递数据,我们可以直接使用 props 来将祖先组件的数据传递给子孙组件: 注:上图来自 Vue.js 官网:Prop ...
- vue.js 组件之间传递数据
前言 组件是 vue.js 最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用.如何传递数据也成了组件的重要知识点之一. 组件 组件与组件之间,还存在着不同的关 ...
- vue组件-构成组件-父子组件相互传递数据
组件对于vue来说非常重要,学习学习了基础vue后,再回过头来把组件弄透! 一.概念 组件意味着协同工作,通常父子组件会是这样的关系:组件 A 在它的模版中使用了组件 B . 它们之间必然需要相互通信 ...
- vue 父子组件相互传递数据
例子一 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...
- vue组件 Prop传递数据
组件实例的作用域是孤立的.这意味着不能(也不应该)在子组件的模板内直接引用父组件的数据.要让子组件使用父组件的数据,我们需要通过子组件的props选项. prop 是单向绑定的:当父组件的属性变化时, ...
- Spring 跨重定向请求传递数据
在处理完POST请求后, 通常来讲一个最佳实践就是执行一下重定向.除了其他的一些因素外,这样做能够防止用户点击浏览器的刷新按钮或后退箭头时,客户端重新执行危险的POST请求. 在控制器方法返回的视图名 ...
- SpringMVC跨重定向请求传递数据
(1)使用URL模板以路径变量和查询参数的形式传递数据(一些简单的数据) @GetMapping("/home/index") public String index(Model ...
随机推荐
- windows下pem转ppk
下载puttygen.exe,启动 下载路径:链接:https://pan.baidu.com/s/1hstORTa 密码:kvfi pem -> ppk :通过PuTTYgen 转换 1. I ...
- HTTP RFC解析
HTTP协议(HyperText Transfer Protocol,超文本传输协议)HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出, ...
- 递归&栈帧空间
递归函数: 自己调用自己的函数 def digui(n): print(n) if n > 0: digui(n-1) print(n) digui(5) 执行结果: 5 4 3 2 1 0 0 ...
- WPF 异常其他信息: “对类型“BaseControl.KImgButton”的构造函数执行符合指定的绑定约束的调用时引发了异常。”,行号为“38”,行位置为“22”。
引发的异常:“System.Windows.Markup.XamlParseException”(位于 PresentationFramework.dll 中) 其他信息: “对类型“BaseCont ...
- jq中工作中用到的一些方法总结
1.css : 1.判断:hasClass() 2.添加:addClass() 3.移除:removeClass() 2选择器: 1.获取指定上级 $(this).closest ...
- CSS选择器效率
CSS选择器效率从高到低的排序如下: ID选择器 比如#header 类选择器 比如.promo 元素选择器 比如 div 兄弟选择器 比如 h2 + p 子选择器 比如 li > ul 后代选 ...
- scala变量类型和性质
最高的父类型为Any,最低类型为Nothing Any is the supertype of all types, also called the top type. It defines cert ...
- 启动tomcat时cmd窗口一闪而过
在tomcat的安装目录下 双击startup.bat启动时cmd窗口一闪而过 1.在系统中查看配置JDK的环境变量是否正确 2.进入tomcat的安装目录 在启动tomcat时流程是:startup ...
- GPUImage中曝光滤镜实现——GPUImageExposureFilter
核心代码: varying highp vec2 textureCoordinate; uniform sampler2D inputImageTexture; uniform highp float ...
- python3的命令行参数传递
#coding:utf-8#命令行参数传递,例如输入: python <文件名>.py -help#这个结果就会打印help#sys.argv[0]代表"文件名",第一 ...