Context

Context提供了除props之外的传参数的方式。

Context是全局跨组件传递数据的。

API

  • React.createContext


    const {Provider, Consumer} = React.createContext(defaultValue);
  • Provider


    <Provider value={/* some value */}>
  • Consumer


    <Consumer>
    {value => /* render something based on the context value */}
    </Consumer>

Example

ThemeContext.js


import React from 'react'; export const themes = {
light: {
foreground: '#000000',
background: '#eeeeee',
},
dark: {
foreground: '#ffffff',
background: '#222222',
},
}; export default React.createContext(
themes.dark // default value
);

ThemedButton.jsx


import React from 'react';
import ThemeContext, {themes} from './ThemeContext'; export default ({children}) => {
const styles = {
color: themes[theme].foreground,
backgroundColor: themes[theme].background
};
return (
<ThemeContext.Consumer>
{theme => {
return (
<button style={styles}>{children}</button>
)
}}
</ThemeContext.Consumer>
);
}

App.js


import React, {PureComponent} from 'react';
import ThemeContext from './ThemeContext';
import ThemeButton from './ThemedButton'; export default class extends PureComponent {
constructor(props) {
super(props);
this.state = {theme: 'dark'};
} render() {
return (
<ThemeContext.Provider value={this.state.theme}>
<ThemeButton>
<div onClick={() => {
this.setState({theme: this.state.theme === 'dark' ? 'light' : 'dark'})
}}>Themed Button</div>
</ThemeButton>
</ThemeContext.Provider>
);
}
}

在线示例

推荐阅读《React 手稿》

React手稿 - Context的更多相关文章

  1. React 手稿 - Component state

    Component state 实例: import React, { PureComponent } from 'react'; export default class extends PureC ...

  2. React手稿之 React-Saga

    Redux-Saga redux-saga 是一个用于管理应用程序副作用(例如异步获取数据,访问浏览器缓存等)的javascript库,它的目标是让副作用管理更容易,执行更高效,测试更简单,处理故障更 ...

  3. 手写一个React-Redux,玩转React的Context API

    上一篇文章我们手写了一个Redux,但是单纯的Redux只是一个状态机,是没有UI呈现的,所以一般我们使用的时候都会配合一个UI库,比如在React中使用Redux就会用到React-Redux这个库 ...

  4. React手稿之State Hooks of Hooks

    React Hooks React在16.7.0-alpha.0版本中提到了Hooks的概念,目前还是Proposal阶段. 官方也陈述,接下来的90%的工作会投入到React Hooks中. 从目前 ...

  5. React Hooks & Context API

    React Hooks & Context API responsive website https://reactjs.org/docs/hooks-reference.html https ...

  6. 【react】---context的基本使用新版---【巷子】

    一.全局定义context对象 globalContext.js import React from "react"; const GlobalContext = React.cr ...

  7. 【react】---context的基本使用---【巷子】

    一.context的理解 很多优秀的React组件都通过Context来完成自己的功能,比如react-redux的<Provider />,就是通过Context提供一个全局态的stor ...

  8. React 新 Context API 在前端状态管理的实践

    本文转载至:今日头条技术博客 众所周知,React的单向数据流模式导致状态只能一级一级的由父组件传递到子组件,在大中型应用中较为繁琐不好管理,通常我们需要使用Redux来帮助我们进行管理,然而随着Re ...

  9. React 中 context 的使用

    官方文档说明(英) 看了别人写的中文博客,再看了官方英文文档,发现还是官方文档讲的浅显易懂一些,看了之后,半翻译半理解地写了这篇博客,更易于新手理解. 介绍 context 是在 react @ 0. ...

随机推荐

  1. NOIP2017 D2T1 奶酪

    洛谷P3958 超级水的并没有用什么几何知识的几何题…… 直接爆搜一遍最后判断有没有与上/下表面相连的球之间连通即可……O(n2)不动脑子的复杂度 最多只是用一下并查集来判断两个点是否连通…… 具体细 ...

  2. R语言-三种方法绘制单位圆

    与一般开发语言不同,R以数据统计分析和绘图可视化为主要卖点.本文是第一篇博客,解决一个简单的绘图问题,以练手为目的. 以下直接给出三种单位圆的画法: 方法1 f=seq(,*pi,0.001) x=s ...

  3. compile and link C/CPP programs on Mac

    ref: https://stackoverflow.com/questions/29987716/cannot-use-gsl-library-on-macos-ld-symbols-not-fou ...

  4. sqlite3 on python for newbies

    python 集成了 sqlite3 ,其接口很简单: import sqlite3 db_connection = sqlite3.connect(db_filename) db_cursor = ...

  5. 微信小程序支付功能讲解

    前言:虽然小程序做过很多,但是一直觉得微信支付功能很是神秘,现在终于有机会接触心里还是有点小激动的,经过一番折腾发现支付也不过如此,在此记录下支付功能的实现过程 小程序的官方文档介绍到发起微信支付即调 ...

  6. window 连接服务器工具

    Xshell xftp 下载网址 以上两个软件均免费, 只需要邮件激活即可. 其中 xshell 主要用来连接服务器,方便使用命令行.xftp 方便传输文件.

  7. Spring_Boot 简单例子

    第一步创建项目: 创建项目地址:https://start.spring.io/ 接下来就下载到本地了 跟着加压 接着用idea打开:等待资源下载完成 我写了个简单的:增删改查 项目结构: dao层: ...

  8. 《SQL Server 2012 T-SQL基础》读书笔记 - 10.可编程对象

    Chapter 10 Programmable Objects 声明和赋值一个变量: DECLARE @i AS INT; SET @i = 10; 变量可以让你暂时存一个值进去,然后之后再用,作用域 ...

  9. mysql5.7多实例安装

    [root@vhost1]# cd /opt/source[root@vhost1]#ls mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz[root@vhost1 ...

  10. Jmeter之完整的HTTP接口测试

    目前很多接口都是基于HTTP的,所以针对HTTP接口测试的了解很重要,下面就简单说明一下,一个基于Jmeter上HTTP接口测试需要的内容. 一.一个HTTP接口测试需要最基础的内容 如下: 简单说明 ...