当前位置:网站首页>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
边栏推荐
- 使用 PowerShell 将 Windows 转发事件导入 SQL Server
- Windows 事件查看器记录到 MYSQL
- STM32 GPIO LED and buzzer implementation [Day 4]
- Some optional strategies and usage scenarios for PWA application Service Worker caching
- 如何选择合适的导电滑环型号
- uniapp隐藏导航栏和横屏显示设置
- 纯纯粹粹纯纯粹粹
- TiKV & TiFlash accelerate complex business queries丨TiFlash application practice
- When mobile applications go overseas, is your "network optimization" holding back?
- C专家编程 第2章 这不是Bug,而是语言特性 2.4 少做之过
猜你喜欢

【带你了解SDN和网络虚拟化】

从零开始搭建MySQL主从复制架构

Windows 事件查看器记录到 MYSQL
[redis] cache penetration and cache avalanche and cache breakdown solutions
![STM32 GPIO LED and buzzer implementation [Day 4]](/img/13/dbfed5a207e97ba0b78c1f63712e16.png)
STM32 GPIO LED and buzzer implementation [Day 4]

protobuf 中数据编码规则

MATLAB | 一种简易的随机曼陀罗图形生成函数

如何设计大电流九线导电滑环

自动化部署+整合SSM项目

To participate in sweepstakes, incoming new programmers magazine welfare!
随机推荐
兄弟组件通信context
devops-2:Jenkins的使用及Pipeline语法讲解
【系统学习编程-编程入门-全民编程 视频教程】
Small Tools (4) integrated Seata1.5.2 distributed transactions
deepstresam的插件配置说明,通过配置osd,设置字体的背景为透明
B站回应HR称核心用户是Loser;微博回应宕机原因;Go 1.19 正式发布|极客头条
leetcode:189. 轮转数组
C专家编程 第3章 分析C语言的声明 3.3 优先级规则
C专家编程 第1章 C:穿越时空的迷雾 1.8 ANSI C标准的结构
Web3 安全风险令人生畏?应该如何应对?
使用 PowerShell 将 Windows 转发事件导入 SQL Server
面试不再被吊打!这才是Redis分布式锁的七种方案的正确打开方式
How to analyze the weekly activity rate?
To participate in sweepstakes, incoming new programmers magazine welfare!
自动化部署+整合SSM项目
Big guys.Use flink-cdc-sqlserver version 2.2.0 to read sqlserver2008R
正向代理与反向代理
Looking at the ecological potential of Hongmeng OS from the evolution of MatePad Pro
AI+BI+Visualization, Deep Analysis of Sugar BI Architecture
Some optional strategies and usage scenarios for PWA application Service Worker caching