当前位置:网站首页>sibling component communication context
sibling component communication context
2022-08-03 16:40:00 【It's Zhang Yu Xiao Wanzi Duck】
Introduction to context
In normal use of react, data is transmitted from top to bottom. If data is stored in the state in the top-level component, without any third-party state management framework, you want to store data in the child component.The only way to get the data of the top-level components is to transfer the data layer by layer, even if the components between the two do not need the data at all, if the layer is deeper, the layer-by-layer data transfer can be described as a disaster, so the emergence of context is a disaster.To solve this problem, context is a way to pass values across levels,
context implementation steps
The role of the sibling component is similar to the middleman model, first son to father, then father to son
context state transfer data
First define an empty object, throw a React.createContext (default value), and assign the structure to get the Provider and Consumer components
The root component defines the provider that uses the Context where needed
Define contextType to get data, or you can use Consumer to get multiple content
Code display
context.js component
import React from 'react';//Define the data of an objectlet context = React.createContext(null);// throw the content of the objectexport const {Provider,Consumer} = context;
App.js component
Hierarchical relationship: App->Text->Span
We use the Provider component directly in the top-level component, and the Provider component has a value property for passing the actual value of the context.Then we can get these values to use in the underlying Child component.
import React from 'react';import './App.css';import { Provider, Consumer } from "./context/index";class App extends React.Component {constructor() {super();this.state = {color: "#FF0000"}}changeColor = () => {console.log(this.refs.colors.value)this.setState({color: this.refs.colors.value})}render() {let { color } = this.state;return ({ color: color, fontSize: "24px" }}> )}}function Text(props) {return (Text subcomponent
)}function Span(props) {return ({/* Accept data from Provider */}{(context) => {console.log(context)return (Hello React)}} )}export default App;
Use scenarios:
When we want to pass data across levels, and there are not many places to pass the data itself, we often don't want to introduce a more complex state management framework (such as redux, etc.) at this time, the context will be a verygood choice
边栏推荐
- FinClip | 2022 年 7 月产品大事记
- SwinIR实战:详细记录SwinIR的训练过程
- MySQL窗口函数 PARTITION BY()函数介绍
- Huawei, Lenovo, BAIC, etc. were selected as the first batch of training bases for "Enterprise Digital Transformation and Security Capability Improvement" by the Ministry of Industry and Information Te
- 【AppCube】零代码小课堂开课啦
- [Deep Learning] Today's bug (August 2)
- C专家编程 第1章 C:穿越时空的迷雾 1.6 它很棒,但它符合标准吗
- Analysis of ffplay video playback principle
- Kubernetes 笔记 / 任务 / 管理集群 / 用 kubeadm 管理集群 / 配置一个 cgroup 驱动
- B站回应HR称核心用户是Loser;微博回应宕机原因;Go 1.19 正式发布|极客头条
猜你喜欢
随机推荐
WordPress建站技术笔记
【翻译】关于扩容一个百万级别用户系统的六个课程
To add digital wings to education, NetEase Yunxin released the overall solution of "Internet + Education"
devops-2:Jenkins的使用及Pipeline语法讲解
兄弟组件通信context
Kubernetes 笔记 / 生产环境
C专家编程 第2章 这不是Bug,而是语言特性 2.2 多做之过
详谈RDMA技术原理和三种实现方式
元宇宙系列--Value creation in the metaverse
DataGrip数据仓库工具
C专家编程 第1章 C:穿越时空的迷雾 1.9 阅读ANSI C标准,寻找乐趣和裨益
TiKV & TiFlash 加速复杂业务查询丨TiFlash 应用实践
新版本 MaxCompute 的SQL 中支持的 EXTRACT 函数有什么作用?
甲方不让用开源【监控软件】?大不了我自己写一个
node connection mongoose database process
DAYU200 OpenHarmony标准系统HDMI全屏显示
Kubernetes 笔记 / 入门 / 生产环境 / 容器运行时
When mobile applications go overseas, is your "network optimization" holding back?
TypeScript文件的编译执行
C语言03、数组