当前位置:网站首页>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
边栏推荐
- protobuf 反射使用总结
- EA 改口,称单人游戏是产品组合中“非常重要的一部分”
- Some optional strategies and usage scenarios for PWA application Service Worker caching
- MobileVIT实战:使用MobileVIT实现图像分类
- node连接mongoose数据库流程
- MySQL窗口函数 OVER()函数介绍
- 《社会企业开展应聘文职人员培训规范》团体标准在新华书店上架
- 华为、联想、北汽等入选工信部“企业数字化转型和安全能力提升”首批实训基地
- To add digital wings to education, NetEase Yunxin released the overall solution of "Internet + Education"
- Yuan xiaolin: Volvo focus on travel security, and put it perfectly
猜你喜欢

Cookie和Session的关系

leetcode:187. 重复的DNA序列

组件通信--下拉菜单案例

DAYU200 OpenHarmony标准系统HDMI全屏显示

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

详谈RDMA技术原理和三种实现方式

想进阿里?先来搞懂一下分布式事务

leetcode-268.丢失的数字

自动化部署+整合SSM项目

视频人脸识别和图片人脸识别的关系
随机推荐
中小微企业如何简单便捷、低成本实现数字化?360视觉云有妙招
想进阿里?先来搞懂一下分布式事务
MobileVIT实战:使用MobileVIT实现图像分类
yolov5s用自己的数据集进行训练模型
#夏日挑战赛#【FFH】OpenHarmony设备开发基础(四)启动流程
【带你了解SDN和网络虚拟化】
C专家编程 第3章 分析C语言的声明 3.3 优先级规则
DAYU200 OpenHarmony标准系统HDMI全屏显示
Windows 事件转发到 SQL 数据库
高薪程序员&面试题精讲系列132之微服务之间如何进行通信?服务熔断是怎么回事?你熟悉Hystrix吗?
MATLAB | 一种简易的随机曼陀罗图形生成函数
C专家编程 第1章 C:穿越时空的迷雾 1.8 ANSI C标准的结构
TypeScript的配置文件tsconfig.json
设置海思芯片MMZ内存、OS内存详解
兄弟组件通信context
leetcode:202. 快乐数
C语言02、语句、函数
C专家编程 第1章 C:穿越时空的迷雾 1.10 “安静的改变”究竟有多少安静
socket快速理解
Cookie和Session的关系