React Context(一):隐式传递数据
一 Context概述
Context provides a way to pass data through the component tree without having to pass props down manually at every level.
Context 提供了一个无需为每层组件手动添加 props,就能在组件树间进行数据传递的方法。
二 项目结构

三 代码
1 theme-context.js
import React from 'react'; // 主题数据
export const themes = {
gray: {
background: 'gray',
},
gold: {
background: 'gold',
},
}; // 创建上下文对象,参数将作为上下文对象的默认值
export const ThemeContext = React.createContext(
themes.gray // default value
);
2 themed-button.js
import React from 'react';
import { ThemeContext } from './theme-context'; class ThemedButton extends React.Component {
// props默认是空对象{}。
// context默认是空对象{}。绑定上下文对象后,创建组件时,会传入上下文对象的值(就近原则)。
constructor(props,context){
super(props)
console.log(arguments)
}
render() {
let props = this.props;
let theme = this.context;
return (
<button
{...props}
style={{...theme}} />
)
}
} // 组件类绑定上下文对象后,组件对象的context属性才能使用上下文对象的值(就近原则)。
ThemedButton.contextType = ThemeContext; export default ThemedButton;
3 app.js
import React, { Fragment } from 'react';
import { ThemeContext, themes } from './theme-context';
import ThemedButton from './themed-button';
// An intermediate component that uses the ThemedButton
// 工具栏组件(中间件)
function Toolbar(props) {
return (
<ThemedButton onClick={props.changeTheme}>
改变上下文对象
</ThemedButton>
);
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
theme: themes.gray
};
this.toggleTheme = () => {
this.setState(state => ({
theme: state.theme === themes.gray ? themes.gold : themes.gray
}));
}
}
render() {
return (
<Fragment>
{/* 在Provider中,使用Provider提供的值 */}
<ThemeContext.Provider value={this.state.theme}>
<Toolbar changeTheme={this.toggleTheme} />
</ThemeContext.Provider>
{/* 不在Provider中,使用默认值 */}
<section>
<ThemedButton />
</section>
</Fragment>
);
}
}
export default App;
4 index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App'; ReactDOM.render(<App />, document.getElementById('root'));
四 运行效果
1 初始化

2 点击按钮

React Context(一):隐式传递数据的更多相关文章
- React之使用Context跨组件树传递数据
--------------------------------- 讲解一 原文:https://blog.csdn.net/xuxiaoping1989/article/details/78480 ...
- react 基础语法复习3- 数据传递 & 数据变化(props&&state)
react当中的数据传递是通过 props以及 state来实现的 props <Header name="kugeliu" /> Header组件当中有个name属性 ...
- React中的Context——从父组件传递数据
简介:在React中,数据可以以流的形式自上而下的传递,每当你使用一个组件的时候,你可以看到组件的props属性会自上而下的传递.但是,在某些情况下,我们不想通过父组件的props属性一级一级的往下传 ...
- JavaScript 数据类型转换(显式与隐式)
一.数据类型 JS中有5中简单数据类型(也称为基本数据类型):Undefined.Null.Boolean.Number.String.还有一种复杂数据类型------Object,Object本质是 ...
- Spring接收数据,传递数据
Spring接收数据,传递数据 前提配置 POM <dependency> <groupId>org.springframework</groupId> < ...
- Scala 学习之路(十三)—— 隐式转换和隐式参数
一.隐式转换 1.1 使用隐式转换 隐式转换指的是以implicit关键字声明带有单个参数的转换函数,它将值从一种类型转换为另一种类型,以便使用之前类型所没有的功能.示例如下: // 普通人 clas ...
- Scala 系列(十三)—— 隐式转换和隐式参数
一.隐式转换 1.1 使用隐式转换 隐式转换指的是以implicit关键字声明带有单个参数的转换函数,它将值从一种类型转换为另一种类型,以便使用之前类型所没有的功能.示例如下: // 普通人 clas ...
- java this 隐式参数
第37级 this 是隐式参数, 类的方法调用时,会系统自动传递一个this的参数给方法.(这个参数是隐式传递的) 所以在方法里可以使用this这个参数. this在方法中表示对象. this(参数列 ...
- dubbo之隐式参数
隐式参数 可以通过 RpcContext 上的 setAttachment 和 getAttachment 在服务消费方和提供方之间进行参数的隐式传递. 在服务消费方端设置隐式参数 setAttach ...
随机推荐
- python trie
Trie 库 https://github.com/pytries/marisa-trie/blob/master/docs/tutorial.rst http://marisa-trie.readt ...
- 优雅的使用列表推导式和lambda
按照条件过滤列表中的数据 过滤出列表中以"KLL","KBB","KHH"开头的数据 1.使用列表推导式 [orderNoOrReturnN ...
- PythonStudy——赋值运算符 Assignment operator
eg: num = 10 num += 1 # 等价于 num = num + 1 => 11 print(num) 特殊操作: 1.链式赋值 a = b = num print(a, b, n ...
- golang-grpc-Unimplemented-desc
golang 调用grpc 服务方法时候提示:"rpc error: code = Unimplemented desc ="的错误, 这是由于pb中的package name 被 ...
- gitlab部署步骤+汉化
系统环境centos7 下载gitlab安装包 https://packages.gitlab.com/gitlab/gitlab-ce 我下载的版本是 gitlab-ce-11.1.4-ce.0.e ...
- Linux内核分析第三次作业
实验:mykernel时间片轮转多道程序内核 进入实验楼实验,在终端中分别输入以下命令 cd LinuxKernel/linux-3.9.4 rm -rf mykernel patch -p1 < ...
- Linux eclipse 编译C++
1.软件安装 2.新建C++工程 3.输入新建文件夹的名字 4.新建main.cpp文件 5.编辑main.cpp #include<iostream> int main(){ std:: ...
- HTTP客户端/服务端 POST/GET
获取GET请求内容 实例 //引入模块var http=require('http');var urls=require('url');var util=require('util');//创建服务h ...
- [UE4]Scroll Box带滚动条的容器
一.黑边,当可以往下滚动的时候,下边会出现黑边.当可以往上滚动的时候,上边也会出现黑边. Scroll Box.Style.Style:也可以自定义上下左右黑边的样式: 二.Scroll Box. ...
- 使用RecyclerView实现聊天界面
原文地址:https://blog.csdn.net/wang_wy/article/details/79032698