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 是组件对内的接口.组件内可以引用其他组件,组件之间的引用形成了一个树状结构(组件树),如果下 ...
随机推荐
- [Codeforces 715C] Digit Tree
[题目链接] https://codeforces.com/contest/715/problem/C [算法] 考虑点分治 一条路径(x , y)合法当且仅当 : d(x) * 10 ^ dep(x ...
- EasyUI 下载与引用
1.官网下载地址: http://www.jeasyui.com/download/index.php 一般下载 “GPL Edition” (开源版本). 2.目录结构: demo:案例,可以删 l ...
- java如何写接口给别人调用
参考:https://blog.csdn.net/greatkendy123/article/details/52818466 java web开发(二) 接口开发
- 3-3if-else条件结构 & 3-4 & 3-5
新建类: ConditionDemo2 package com.imooc.operator; public class ConditionDemo2 { public static void mai ...
- QDUOJ LC的课后辅导 单调递增栈
LC的课后辅导 发布时间: 2015年9月19日 21:42 时间限制: 1000ms 内存限制: 256M 描述 有一天,LC给我们出了一道题,如图: 这个图形从左到右由若干个 宽为1 高不 ...
- C#简单的国际化
1.新建一个资源文件夹,并在资源文件夹新建中英问的资源文件,如图: 2.中英文资源文档添加资源,如图: 3.Program.cs中添加根据系统语言确定中英文,这里默认为英文: using Intern ...
- 查看python 3中的内置函数列表,以及函数功能描述
>>> dir(__builtins__)//查看内置函数(BIF)列表 ['ArithmeticError', 'AssertionError', 'AttributeError' ...
- Fitnesse-The Slim Tables
Fitnesse 中Slim支持的表格类型 下表内容路径 Decision Table Supplies the inputs and outputs for decisions. This is s ...
- HDU1597【二分瞎搞】
题意: 求第n个数: 思路: 可以看到一种序列: 1 12 123 1234 12345 123456 1234567 12345678 123456789 1234567891 1234567891 ...
- [Xcode 实际操作]二、视图与手势-(10)UITapGestureRecognizer手势之单击
目录:[Swift]Xcode实际操作 本文将演示使用视图的手势功能,完成视图的交互操作. import UIKit class ViewController: UIViewController { ...