react中的context的基础用法
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的基础用法的更多相关文章
- React中的Context——从父组件传递数据
简介:在React中,数据可以以流的形式自上而下的传递,每当你使用一个组件的时候,你可以看到组件的props属性会自上而下的传递.但是,在某些情况下,我们不想通过父组件的props属性一级一级的往下传 ...
- React中使用styled-components的基础使用
今天准备来给大家分享分享React中styled-components的基础使用,仅仅是我个人的一些理解,不一定全对,有错误还请大佬们指出,496838236这是我qq,有想指点我的大佬随时加我qq好 ...
- react中对于context的理解
一.context旧版的基本使用 1.context的理解 当不想在组件树中通过逐层传递props或state的方式来传递数据时,可使用context来实现跨层级的组件数据传递. 2.context的 ...
- React中ref的三种用法 可以用来获取表单中的值 这一种类似document.getXXId的方式
import React, { Component } from "react" export default class MyInput extends Component { ...
- python中在计算机视觉中的库及基础用法
基于python脚本语开发的数字图像处理包有很多,常见的比如PIL.Pillow.opencv.scikit-image等.PIL和pillow只提供了基础的数字图像处理,功能有限:OpenCV实际上 ...
- react基础用法一(在标签中渲染元素)
react基础用法一(渲染元素) 如图所示最简单的变量使用方法 格式 let 变量名称 = 赋值: 渲染格式直接用 {变量名称} 就可以直接渲染到页面 如图所示第二种渲染方法 格式 const 变量名 ...
- Spring中JdbcTemplate的基础用法
Spring中JdbcTemplate的基础用法 1.在DAO中使用JdbcTemplate 一般都是在DAO类中使用JdbcTimplate,在XML配置文件中配置好后,可以在DAO中注入即可. 在 ...
- Vue基础01vue的基本示例,vue的双向数据绑定,vue中常见的几种用法,vue相关常见指令
自学vue框架,每天记录重要的知识点,与大家分享!有不足之处,希望大家指正. 本篇将讲述:vue的基本示例,vue的双向数据绑定,vue中常见的几种用法,vue相关常见指令 前期学习基础,使用vue. ...
- React中Props 和 State用法
React中Props 和 State用法 1.本质 一句话概括,props 是组件对外的接口,state 是组件对内的接口.组件内可以引用其他组件,组件之间的引用形成了一个树状结构(组件树),如果下 ...
随机推荐
- bzoj 2194 快速傅立叶之二 —— FFT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2194 如果把 a 序列翻转,则卷积得到的是 c[n-i],再把得到的 c 序列翻转即可. 代 ...
- TFS独占签出代码
最近发现微软给我们提供了免费的TFS,地址:http://tfs.visualstudio.com/, 就注册了一个,但是我发现没办法独占签出. 在公司里,TFS有服务端,所以很好设置,但是注册微软的 ...
- ascall文件和二进制文件
ascall文件可以打开让我们看你们的具体内容. 二进制文件打开我们看到的就是一堆乱码. ascall在换行时不同的平台不一样: windows上面用 \r\n linux上面用 \n 二进制的内容 ...
- jquery data 选择器 表格序列化serialize()
data()在元素上存放或者读取数据,返回jquery对象. demo: <div data-obj="{'name':'zhangsan','age':20}">&l ...
- CS231n 2016 通关 第三章-Softmax 作业
在完成SVM作业的基础上,Softmax的作业相对比较轻松. 完成本作业需要熟悉与掌握的知识: cell 1 设置绘图默认参数 mport random import numpy as np from ...
- 【Data Structure & Algorithm】求1+2+…+n
求1+2+-+n 题目:求1+2+-+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A ? B : C). 分析:此题没多少实际意义,因为 ...
- hql实现对表的某几个(部分)字段查询
如何利用hql实现对表的部分字段查询 假如,我们有一张person表,对应实体类Person,表中有字段name,age,sex,address 哪我们如何来实现全部和部份字段的查询呢? hql的写法 ...
- LeetCode: 463 Island Perimeter(easy)
题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...
- ZOJ3321,ZOJ3317
ZOJ3321 //there is at most one edge between two nodes. 因为这句话的局限性,又要满足环,那么一定是每个点度为2,然后为n节点的一个环 //#inc ...
- 3DMAX 如何将删去的面补回来
1.例如下面长方体被删除一个面 2.点击主键盘区数字键[3] ,进入[边界]修改模式 ,使用鼠标点击 被删除面的边界,并点击[修改面板]---[封口],例如下图: