context提供了一种数据共享的机制,里面有两个关键概念——provider,consumer,下面做一些key features描述。

参考网址:https://react.docschina.org/docs/context.html#reactcreatecontext

consumer:

数据消费者,消费来自己向上回溯过程中,离自己最近的provider提供的数据。

consumer接收一个函数作为子节点,函数返回一个react节点;函数可以消费来自context的值(即最近provider提供的数据)进行内部JSX语法渲染。

provider:

数据提供者,一个provider下的所有消费者都可以消费来自该provider的数据。

provider接收一个value属性,这个属性讲被provider提供给其consumer子节点。

一个provider可对应多个consumer。

生成一对{ provider, consumer}

基本上关于Context相关的主要特性都介绍完了,一个完整的demo如下:

theme-context.js

export const themes = {
light: {
foreground: '#ffffff',
background: '#222222',
},
dark: {
foreground: '#000000',
background: '#eeeeee',
},
};
export const ThemeContext = React.createContext(
themes.light //默认值
);

themed-button.js

import React from 'react';
import {themes,ThemeContext} from './theme-context';
class ThemeCircle extends React.Component{
render(){
return (<ThemeContext.Consumer>
{ theme => (<div
style={{
width:"60px",
height:"60px",
borderRadius:"30px",
backgroundColor: theme.background}}>
{/* Circle */}
</div>) }
</ThemeContext.Consumer>
);
}
}
class ThemedButton extends React.Component{
constructor( props ){
super(props);
}
render(){
return (<ThemeContext.Consumer>
{ theme => (<button
onClick = {this.props.changeTheme}
{...this.props}
style={{
backgroundColor:theme.background,
color:theme.foreground,
width:"100px",
height:"45px",
fontSize:"14px",
borderRadius:"5px",
border:"none"}}
/>) }
</ThemeContext.Consumer>);
}
}
// class ToolBar extends React.Component{//一个用到ThemedButton的中间件
// constructor( props ){
// super(props);
// }
// render(){//以此种方式直接触发父级事件?
// return (<ThemedButton onClick={this.props.changeTheme}>
// Change Theme
// </ThemedButton>);
// }
// }
class CustomizedApp extends React.Component{
constructor( props ){
super(props);
this.state = {
theme: themes.light
};
this.handleChangeTheme = this.handleChangeTheme.bind( this );
}
handleChangeTheme(){
//必须有一个机制可以修改provider的value,以引起订阅者consumer的变更
this.setState( state => ({
theme: state.theme === themes.light?themes.dark:themes.light
}) );
}
render(){
return (<div>
<ThemeContext.Provider value={this.state.theme}>
{/* <ToolBar changeTheme={this.handleChangeTheme} /> */}
<ThemedButton changeTheme={this.handleChangeTheme}>
Toggle
</ThemedButton>
<ThemeCircle />
</ThemeContext.Provider>
</div>);
}
}
export default CustomizedApp;

然后在Router里配置调用“CustomizedApp”组件就可以了。

路漫漫其修远其,吾将上下而求索——May stars guide your way.

react中的context的基础用法的更多相关文章

  1. React中的Context——从父组件传递数据

    简介:在React中,数据可以以流的形式自上而下的传递,每当你使用一个组件的时候,你可以看到组件的props属性会自上而下的传递.但是,在某些情况下,我们不想通过父组件的props属性一级一级的往下传 ...

  2. React中使用styled-components的基础使用

    今天准备来给大家分享分享React中styled-components的基础使用,仅仅是我个人的一些理解,不一定全对,有错误还请大佬们指出,496838236这是我qq,有想指点我的大佬随时加我qq好 ...

  3. react中对于context的理解

    一.context旧版的基本使用 1.context的理解 当不想在组件树中通过逐层传递props或state的方式来传递数据时,可使用context来实现跨层级的组件数据传递. 2.context的 ...

  4. React中ref的三种用法 可以用来获取表单中的值 这一种类似document.getXXId的方式

    import React, { Component } from "react" export default class MyInput extends Component { ...

  5. python中在计算机视觉中的库及基础用法

    基于python脚本语开发的数字图像处理包有很多,常见的比如PIL.Pillow.opencv.scikit-image等.PIL和pillow只提供了基础的数字图像处理,功能有限:OpenCV实际上 ...

  6. react基础用法一(在标签中渲染元素)

    react基础用法一(渲染元素) 如图所示最简单的变量使用方法 格式 let 变量名称 = 赋值: 渲染格式直接用 {变量名称} 就可以直接渲染到页面 如图所示第二种渲染方法 格式 const 变量名 ...

  7. Spring中JdbcTemplate的基础用法

    Spring中JdbcTemplate的基础用法 1.在DAO中使用JdbcTemplate 一般都是在DAO类中使用JdbcTimplate,在XML配置文件中配置好后,可以在DAO中注入即可. 在 ...

  8. Vue基础01vue的基本示例,vue的双向数据绑定,vue中常见的几种用法,vue相关常见指令

    自学vue框架,每天记录重要的知识点,与大家分享!有不足之处,希望大家指正. 本篇将讲述:vue的基本示例,vue的双向数据绑定,vue中常见的几种用法,vue相关常见指令 前期学习基础,使用vue. ...

  9. React中Props 和 State用法

    React中Props 和 State用法 1.本质 一句话概括,props 是组件对外的接口,state 是组件对内的接口.组件内可以引用其他组件,组件之间的引用形成了一个树状结构(组件树),如果下 ...

随机推荐

  1. bzoj 4503 两个串 —— FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4503 推式子即可: 不知怎的调了那么久,应该是很清晰的. 代码如下: #include< ...

  2. centos7使用ceph-deploy部署ceph

    准备阶段 准备yum源 删除默认的源,国外的比较慢 yum clean all rm -rf /etc/yum.repos.d/*.repo 下载阿里云的base源 wget -O /etc/yum. ...

  3. 在xshell中使用Linux语言打开错误提示

    上线项目到服务器后, 有时候有的功能跟本地调试的不一样,这时候就需要设置打开display_errors = On: 首先,cd .. 进入上一级,ll 罗列当前目录,跟home当前目录的有这个usr ...

  4. Beyond Compare 简体版+注册码

    Beyond Compare 3.3.4.14431 官方简体版+注册码 查阅全文 ›

  5. 关于window.event.srcElement 和 window.event.target(触发事件的对象)

    转自:https://www.cnblogs.com/zhilingege/p/7423817.html window.event.srcElement   是指触发事件的对象 <script ...

  6. Flutter实战视频-移动电商-28.列表页_商品列表后台接口调试

    28.列表页_商品列表后台接口调试 主要调试商品列表页的接口 这个接口是最难的因为有大类.小类还有上拉加载 先配置接口 config/service_url.dart //const serviceU ...

  7. shell脚本函数与数组

    前言 之前写过一篇关于shell脚本流程控制总结,这次继续写关于shell脚本的问题.本篇文章主要包含shell脚本中的函数以及数组的用法介绍.同时也涵盖了一些字符串处理以及shell脚本比较使用的小 ...

  8. IE8 以上版本兼容

    在html的内如下写法 其中最后一行是永远以最新的IE版本模式来显示网页的. 另外加上Emulate模式 Emulate模式后则更重视 (细心的人会注意到,用IE9去访问带有x-ua-compatib ...

  9. Linux下共享库嵌套依赖问题 (转载)

    转自:http://my.oschina.net/moooofly/blog/506466 问题场景: 动态库 librabbitmq_r.so 内部依赖动态库 libevent_core.so 和 ...

  10. javascript runat server

    <script runat="server"> protected void SubmitBtn_Click(object sender, EventArgs e) { ...