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. Linux安装php扩展memcache

    Linux安装php扩展memcache   php扩展memcache的作用是为了支持memcached数据库缓存服务器,下面是安装方法.1.下载并解压memcache文件 wget -c http ...

  2. django orm(2)

    目录 聚合函数 分组查询 F与Q查询 F查询 Q查询 事务 Django中的事务 orm字段及参数 自定义char字段 聚合函数 这里的聚合函数和SQL里的聚合函数对应,在使用前需要先进行模块的导入: ...

  3. 【leetcode】540. Single Element in a Sorted Array

    题目如下: 解题思路:题目要求时间复杂度是O(logN),可以尝试使用二分查找法.首先数组是有序的,而且仅有一个元素出现一次,其余均为两次.我们可以先找到数组最中间的元素,记为mid.如果mid和mi ...

  4. PHP 大文件上传进度条实现

    核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...

  5. 题解 P2674 【《瞿葩的数字游戏》T2-多边形数】

    题目说了很清楚,此题找规律,那么就找规律. 我们观察数列. 令k表示数列的第k个数. 三角形数:1 3 6 10 15 两项相减:1 2 3 4 5 再次相减:1 1 1 1 1 四边形数:1 4 9 ...

  6. kohana 语言资源国际化、本地化

    语言配置开关: root@DESKTOP-I4OIMJC /cygdrive/e/html/tproject/framebota/platform # grep -n2 'I18n::lang' bo ...

  7. Oracle And子句

    Oracle And子句 作者:初生不惑 Oracle基础 评论:0 条 Oracle技术QQ群:175248146 在本教程中,将学习如何使用Oracle AND运算符来组合两个或更多的布尔表达式. ...

  8. 大数据笔记(十八)——Pig的自定义函数

    Pig的自定义函数有三种: 1.自定义过滤函数:相当于where条件 2.自定义运算函数: 3.自定义加载函数:使用load语句加载数据,生成一个bag 默认:一行解析成一个Tuple 需要MR的ja ...

  9. Xcode磁盘空间清理

    http://www.iwangke.me/2013/09/09/clean-xcode-to-free-up-disk-space/#jtss-tsina 这个目录下面的文件也可以隔一段儿时间清理一 ...

  10. Microsoft windows terminal

    https://github.com/microsoft/terminal 尝试在windows store中安装,结果everything搜索不到 I tried running WindowsTe ...