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 ...
随机推荐
- [UE4]修改射击方向
- Rehash死锁的问题
为什么都说HashMap是线程不安全的呢?它在多线程环境下,又会发生什么情况呢? resize死循环 我们都知道HashMap的初始容量是16,一般来说,当插入数据时,都会检查容量有没有超过设定的th ...
- github----awesome-typescript-projects
https://github.com/brookshi/awesome-typescript-projects
- CRM 2016 一个IFrame_A 刷新另一个 IFrame_B
思路是 : 1 创建一个字段“new_xxx”. 2 注册字段 OnChange 事件. 3 OnChange 事件 刷新 IFrame_B子页面. CRM父页面JS: /// <summary ...
- 通过创建临时表合并某一个库的hive小文件
#!/bin/bash #需要指定hive中的库名 #set -x set -e DB=$1 if [ -z $1 ];then echo "Usage:$0 DbName" ex ...
- 《Linux 性能及调优指南》2.4 基准工具
翻译:飞哥 (http://hi.baidu.com/imlidapeng) 版权所有,尊重他人劳动成果,转载时请注明作者和原始出处及本声明. 原文名称:<Linux Performance a ...
- bzoj3252: 攻略(贪心)
/* 因为权值都是正的, 所以贪心的正确性能保证 然后重链贪心跑一下就好了 */ #include<cstdio> #include<algorithm> #include&l ...
- SpringBoot项目启用本地Tomcat
1.修改pom.xml文件,配置<packaging>war</packaging>,使其发布的时候打包成war包 <groupId>com.owlforest&l ...
- 关于 java jdk 环境变量的配置
最近在学习java , 关于java 环境变量的配置,我想总结一下自己在这方面的经验,可供大家参考: 右键单击“我的电脑” --->"属性” ---> "高级属性设置& ...
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...