记一次 React context 使用
学习 React 之 Context 使用
记录一次React context 使用
React.createContext Api
- 新建文件 contexts.js 文件用来存放 context 对象
import React from "react";
const ThemetContext = React.createContext(defalutValue);
创建一个 Context 对象。当 React 渲染一个订阅了这个 Context 对象的组件,这个组件会从组件树中离自身最近的那个匹配的 Provider 中读取到当前的 context 值。只有当组件所处的树中没有匹配到 Provider 时,其 defaultValue 参数才会生效
新建文件context/indexjs 在组建中获取 context 值
import React from "react";
// 注意这里引入 ThemeContext
import {ThemeContext} from "../contexts";
class Button extends React.Component{
constructor(props) {
super(props);
}
componentDidMount() {
console.log('-------------------------------- Button this --------------------------------', this.props.theme);
}
render() {
return <div>{this.context} {this.props.theme}</div>
}
}
// 挂载在 class 上的 contextType
Button.contextType = ThemeContext;
class Context extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<Button theme={this.context}></Button>
</div>
)
}
}
// 挂载在 class 上的 contextType
Context.contextType = ThemeContext;
export default Context;
挂载在 class 上的 contextType 属性会被重赋值为一个由 React.createContext() 创建的 Context 对象。这能让你使用 this.context 来消费最近 Context 上的那个值。你可以在任何生命周期中访问到它,包括 render 函数中
新建文件 theme/index.js
import React from "react";
import ContextComponent from "../context";
class Theme extends React.Component{
render() {
return (
<div>
<ContextComponent></ContextComponent>
</div>
)
}
}
export default Theme;
- 在 App.js 中使用
import React from "react";
import Theme from "./views/theme";
// 引入 ThemeContext
import {ThemeContext} from "./views/contexts";
function App() {
return (
<div className="App">
<ThemeContext.Provider value="dark">
<Theme></Theme>
</ThemeContext.Provider>
</div>
);
}
export default App;
每个 Context 对象都会返回一个 Provider React 组件,它允许消费组件订阅 context 的变化。
Provider 接收一个 value 属性,传递给消费组件。一个 Provider 可以和多个消费组件有对应关系。多个 Provider 也可以嵌套使用,里层的会覆盖外层的数据。
当 Provider 的 value 值发生变化时,它内部的所有消费组件都会重新渲染。Provider 及其内部 consumer 组件都不受制于 shouldComponentUpdate 函数,因此当 consumer 组件在其祖先组件退出更新的情况下也能更新。
注:再使用过程中碰到了是 render 中 不能直接 this.props 会提示错误,意思 this.props 是一个 Object 对象不能直接在 div 标签中使用
详细内容查看 官方文档
记一次 React context 使用的更多相关文章
- React context基本用法
React的context就是一个全局变量,可以从根组件跨级别在React的组件中传递.React context的API有两个版本,React16.x之前的是老版本的context,之后的是新版本的 ...
- [React] Prevent Unnecessary Rerenders of Compound Components using React Context
Due to the way that React Context Providers work, our current implementation re-renders all our comp ...
- React Context API
使用React 开发程序的时候,组件中的数据共享是通过数据提升,变成父组件中的属性,然后再把属性向下传递给子组件来实现的.但当程序越来越复杂,需要共享的数据也越来越多,最后可能就把共享数据直接提升到最 ...
- 探索 Redux4.0 版本迭代 论基础谈展望(对比 React context)
Redux 在几天前(2018.04.18)发布了新版本,6 commits 被合入 master.从诞生起,到如今 4.0 版本,Redux 保持了使用层面的平滑过渡.同时前不久, React 也从 ...
- React Hooks +React Context vs Redux
React Hooks +React Context vs Redux https://blog.logrocket.com/use-hooks-and-context-not-react-and-r ...
- [译]React Context
欢迎各位指导与讨论 : ) 前言 由于笔者英语和技术水平有限,有不足的地方恳请各位指出.我会及时修正的 O(∩_∩)O 当前React版本 15.0.1 时间 2016/4/25 正文 React一个 ...
- react context跨组件传递信息
从腾讯课堂看到的一则跨组件传递数据的方法,贴代码: 使用步骤: 1.在产生参数的最顶级组建中,使用childContextTypes静态属性来定义需要放入全局参数的类型 2.在父组件中,提供状态,管理 ...
- [译]迁移到新的 React Context Api
随着 React 16.3.0 的发布,context api 也有了很大的更新.我已经从旧版的 api 更新到了新版.这里就分享一下我(作者)的心得体会. 回顾 下面是一个展示如何使用旧版 api ...
- React Context(一):隐式传递数据
一 Context概述 Context provides a way to pass data through the component tree without having to pass pr ...
- react context toggleButton demo
//toggleButton demo: //code: //1.Appb.js: import React from 'react'; import {ThemeContext, themes} f ...
随机推荐
- [ELK] Docker 运行 Elastic Stack 支持 TLS 的两种简单方式
第一种就是 按照官方文档进行配置,指定证书位置开启. Run the Elastic Stack in Docker with TLS enabled. 第二种就是 9200 端口只暴露给本机,127 ...
- 2019-10-7-WPF-will-break-when-an-exception-be-throw-in-the-StylusPlugIn
title author date CreateTime categories WPF will break when an exception be throw in the StylusPlugI ...
- aspnetcore插件开发dll热加载
该项目比较简单,只是单纯的把业务的dll模块和controller的dll做了一个动态的添加删除处理,目的就是插件开发.由于该项目过于简单,请勿吐槽.复杂的后续可以通过泛型的实体.dto等做业务和接口 ...
- 五:瑞芯微RV1109
瑞芯微RV1109是一款用于工控机或人工智能视觉应用的高性能机器视觉处理器SoC. 参考资料 http://www.neardi.com/news_22/434.html https://www.ro ...
- leaflet 基本案例-图层控件:基础图层+业务图层
可执行示例一个,如下: <!DOCTYPE html> <html> <head> <title>Layers Control Tutorial - L ...
- StarCoder2-Instruct: 完全透明和可自我对齐的代码生成
指令微调 是一种技术,它能让大语言模型 (LLMs) 更好地理解和遵循人类的指令.但是,在编程任务中,大多数模型的微调都是基于人类编写的指令 (这需要很高的成本) 或者是由大型专有 LLMs 生成的指 ...
- Java面试题:线程池内“闹情绪”的线程,怎么办?
在Java中,线程池中工作线程出现异常的时候,默认会把异常往外抛,同时这个工作线程会因为异常而销毁,我们需要自己去处理对应的异常,异常处理的方法有几种: 在传递的任务中去处理异常,对于每个提交到线程池 ...
- 撤销 git commit
目录 文章目录 目录 场景1:撤回 commit,不撤销 git add .,保留代码 场景2:撤回 commit,撤销 git add .,保留代码 场景3:撤销 commit,撤销 git add ...
- k8s证书相关
1.cfssl 字签证书 查看证书 可以使用以下命令查询CFSSL证书是否过期: 复制代码 cfssl certinfo -cert <certificate_file> 其中,< ...
- mysql命令最新
查看授权 mysql> select user,host from mysql.user; +--------+------------+ | user | host | +--------+- ...