react的数据传递 是从父级向子级传递的。通过props。如果是很多组件需要的数据,通过props传递会非常麻烦。这个时候可以使用context。

context需要可以类似于store但是也不能滥用。

react-redux的 <Provider /> ,就是通过 Context 提供一个全局态的 store ,路由组件react-router通过 Context 管理路由状态等等。

context适用场景:地区偏好,UI主题

传统实现:

class App extends React.Component {
render() {
return <Toolbar theme="dark" />;
}
} function Toolbar(props) {
// Toolbar 组件接受一个额外的“theme”属性,然后传递给 ThemedButton 组件。
// 如果应用中每一个单独的按钮都需要知道 theme 的值,这会是件很麻烦的事,
// 因为必须将这个值层层传递所有组件。
return (
<div>
<ThemedButton theme={props.theme} />
</div>
);
} class ThemedButton extends React.Component {
render() {
return <Button theme={this.props.theme} />;
}
}

创建context对象

const MyContext = React.createContext(defaultValue);
创建一个context对象。设置默认值。

当 React 渲染一个订阅了这个 Context 对象的组件,这个组件会从组件树中离自身最近的那个匹配的 Provider 中读取到当前的 context 值。如果没有Provider就会取默认值。

context.Provider

<MyContext.Provider value={默认值}>

每个context对象都返回一个provider组件

一个provider组件可以和多个消费组件对应关系。

<ThemeContext.Provider value="dark">
<Toolbar />
<ContextTypePage /> //多个组件
</ThemeContext.Provider>

多个provider也可以嵌套,里层会覆盖外层。

import {ThemeContext, UserContext} from "../Context";
export default function UseContextPage(props) {
const themeContext = useContext(ThemeContext);
const { themeColor } = themeContext;
const userContext = useContext(UserContext);
return (
<div className="border">
<h3 className={themeColor}>UseContextPage</h3>
<p>{userContext.name}</p>
</div>
);
}

  

provider值变化 里面所有的消费组件都会渲染。

组件的contextType属性

挂载在 class 上的 contextType 属性会被重赋值为一个 Context 对象。这能让你使用 this.context 来消费最近 Context 上的那个值。你可以在任何生命周期中访问到它。

import {ThemesContext} from './theme-context'

export class ThemedButton extends React.Component{
render(){
let props =this.props;
let theme =this.context;
console.log(theme)
return (
<button
{...props}
style={{backgroundColor:theme.background,color:theme.foreground,width:"200px"}}
/>
)
}
}
ThemedButton.contextType=ThemesContext;//这个一定要有

简单的例子:

context实现:

// Context 可以让我们无须明确地传遍每一个组件,就能将值深入传递进组件树。
// 为当前的 theme 创建一个 context(“light”为默认值)。
const ThemeContext = React.createContext('light');
class App extends React.Component {
render() {
// 使用一个 Provider 来将当前的 theme 传递给以下的组件树。
// 无论多深,任何组件都能读取这个值。
// 在这个例子中,我们将 “dark” 作为当前的值传递下去。
return (
<ThemeContext.Provider value="dark">
<Toolbar />
          <ContextTypePage /> //多个组件
      </ThemeContext.Provider>
);
}
} // 中间的组件不必指明往下传递 theme 。
function Toolbar() {
return (
<div>
<ThemedButton />
</div>
);
} class ThemedButton extends React.Component {
// 指定 contextType 读取当前的 theme context。
// React 会往上找到最近的 theme Provider,然后使用它的值。
// 在这个例子中,当前的 theme 值为 “dark”。
static contextType = ThemeContext;
render() {
return <Button theme={this.context} />;
}
}

实际应用:

theme-context.js

import React from 'react';
export const themes={
light:{
foreground: '#ffffff',
background:"#4DB8C6"
},
dark:{
foreground: '#ffffff',
background:"#222222"
}
}
export const ThemesContext =React.createContext(themes.dark)

themed-button.js

import React from 'react';
import {ThemesContext} from './theme-context' export class ThemedButton extends React.Component{
render(){
let props =this.props;
let theme =this.context;
console.log(theme)
return (
<button
{...props}
style={{backgroundColor:theme.background,color:theme.foreground,width:"200px"}}
/>
)
}
}
ThemedButton.contextType=ThemesContext;

app.js

function ToolBar1(props){
return (
<ThemedButton onClick={props.changeTheme}>
主题按钮
</ThemedButton>
)
}
class App extends React.Component{
constructor(props){
super(props);
this.state={
theme:themes.light
}
this.toggleTheme = () => {
this.setState(state => ({
theme:
state.theme === themes.dark
? themes.light
: themes.dark,
}));
console.log(this.state.theme)
};
}
render (){
return (
<ThemesContext.Provider value={this.state.theme}>
<ToolBar1 changeTheme={this.toggleTheme} />
</ThemesContext.Provider>
)
}
}

效果图

最好实现主题变动的是 改变类名

写两套css样式 根据不同的类名 进行处理。

react官网 https://react.docschina.org/docs/context.html#when-to-use-context

利用context组件数据传递的更多相关文章

  1. vue教程3-05 vue组件数据传递、父子组件数据获取,slot,router路由

    vue教程3-05 vue组件数据传递 一.vue默认情况下,子组件也没法访问父组件数据 <!DOCTYPE html> <html lang="en"> ...

  2. React中父子组件数据传递

    Vue.js中父子组件数据传递:Props Down ,  Events Up Angular中父子组件数据传递:Props Down,  Events  Up React中父子组件数据传递:Prop ...

  3. 利用 postMessage 进行数据传递 (iframe 及web worker)及问题

    一 postMessage应用于主页面和iframe之间进行数据的传递 1  子iframe页面向主页面进行数据传递: // 多个子iframe需要将自己的计数统计到主页面进行数据上报 window. ...

  4. vue 组件数据传递

    vue组件化开发 主要为了把一个大功能拆分成若干个小的功能,解决高耦合问题,同时也方便开发人员维护.   从功能上组件可以分为木偶组件和功能组件. 木偶组件(为了接收数据,渲染数据,基本上是没有逻辑的 ...

  5. Android开发探秘之四:利用Intent实现数据传递

    在Android开发过程中,很多人都熟悉Intent,这是个用于在多个View之间共享数据的类.本节主要是继承上节,通过点选ListView中的文本,把文本中的URL加载到一个新的页面上,并且打印出来 ...

  6. Vue2.x中的父组件数据传递至子组件

    父组件结构 template <template> <div> <v-girl-group :girls="aGirls"></v-gir ...

  7. vue2.0 父子组件数据传递prop

    vue的一个核心概念就是组件,而组件实例的作用域是孤立的,所以组件之间是不能直接引用其他组件的数据的.极端点举例来说,就是可以在同一个项目中,每一个组件内都可以定义相同名称的数据. data () { ...

  8. Vue2.x之父子组件数据传递

    父传子,并且通过fatherEvent接收子组件传过来的值 <template> <div class='father'> <Son :fatherData=" ...

  9. react父子组件数据传递

    子传父 1.首先父组件设定一个自定义函数 getChildDatas = (values) => { //...业务代码 } 2.将该函数暴露在子组件的引用上 <Child getChil ...

  10. 关于vue.js父子组件数据传递

    vue.js中使用props down,events up的原则进行父子组件间的通信,先来记录下props down,看个例子: <div id="app2"> < ...

随机推荐

  1. 委派模式——从SLF4J说起

    作者:vivo 互联网服务器团队- Xiong yangxin 将某个通用解决方案包装成成熟的工具包,是每一个技术建设工作者必须思考且必须解决的问题.本文从业内流行的既有工具包入手,解析实现思路,沉淀 ...

  2. 编程思想转换-Lambda表达式

    编程思想转换 做什么,而不是怎么做 我们真的希望创建一个匿名内部类对象吗?不.我们只是为了做这件事情而不得不创建一个对象.我们真正希望做的事情是︰将run方法体内的代码传递给 Thread类知晓. 传 ...

  3. 笔记:C#Datatable 根据某字段数量 自动复制该行的数量

    /// <summary> /// 根据Datatable某字段数量自动复制该行查询 /// </summary> /// <param name="dt&qu ...

  4. 【一句话】Thread.sleep(0)的作用

    首先一句话: 在循环中加入Thread.sleep(0),用于在循环中放入safepoint,JVM进行STW,然后触发GC. 详细: 说白了,它的作用就是给可数(int控制)循环加入safepoin ...

  5. Emacs单文件配置

    编辑 ~/.emacs 输入以下内容 ;; 编码 (set-language-info "UTF-8" 'coding-priority '(utf-8 gb18030 gbk g ...

  6. spring中Utils工具类注入问题

    使用工具类的时候,我们想在static修饰的方法中,通过注入来调用其他方法,这里就存在问题. 第一:普通工具类是不在spring的管理下,spring不会依赖注入 第二:即便使用@Autowired完 ...

  7. 万字长文概述单目3D目标检测算法

    一,理论基础-相机与图像 1.1,单目相机介绍 1.2,针孔相机模型 1.3,坐标系间的欧式变换 1.4,世界坐标与像素坐标的转换 1.5,三维旋转:欧拉角.旋转矩阵之间的转换 二,单目3D目标检测概 ...

  8. Centos7系统编译Hadoop3.3.4

    1.背景 最近在学习hadoop,此篇文章简单记录一下通过源码来编译hadoop.为什么要重新编译hadoop源码,是因为为了匹配不同操作系统的本地库环境. 2.编译源码 2.1 下载并解压源码 [r ...

  9. jmeter常见错误(持续更新)

    1. jmeter java.net.NoRouteToHostException: Cannot assign requested address (Address not available) 压 ...

  10. 记一次google手机恢复出厂设置到root抓包全过程

    前言 开始因为手机密码忘记了,不想重置,不然找店家root的工具都没了,自己也不会google root的操作,之前听说还挺麻烦的.操作了半天好了,确实是挺麻烦的,做个记录. 一.恢复出厂设置 还原教 ...