当前位置:网站首页>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
边栏推荐
猜你喜欢

Why do I strongly recommend using smart async?

protobuf 中数据编码规则

deepstresam的插件配置说明,通过配置osd,设置字体的背景为透明

设置海思芯片MMZ内存、OS内存详解

C专家编程 第3章 分析C语言的声明 3.8 理解所有分析过程的代码段
![STM32 GPIO LED and buzzer implementation [Day 4]](/img/13/dbfed5a207e97ba0b78c1f63712e16.png)
STM32 GPIO LED and buzzer implementation [Day 4]

vector类

MATLAB | 七夕节快到了,还不给朋友安排上这个咕呱小青蛙?

#夏日挑战赛#【FFH】OpenHarmony设备开发基础(四)启动流程

Yuan xiaolin: Volvo focus on travel security, and put it perfectly
随机推荐
C语言04、操作符
20. Valid Parentheses
SwinIR实战:详细记录SwinIR的训练过程
组件通信--下拉菜单案例
B站回应HR称核心用户是Loser;微博回应宕机原因;Go 1.19 正式发布|极客头条
关于oracle表空间在线碎片整理
To participate in sweepstakes, incoming new programmers magazine welfare!
C专家编程 第3章 分析C语言的声明 3.4 通过图标分析C语言的声明
甲方不让用开源【监控软件】?大不了我自己写一个
我写了个”不贪吃蛇“小游戏
带你了解什么是 Web3.0
兄弟组件通信context
TCP 可靠吗?为什么?
MATLAB | 一种简易的随机曼陀罗图形生成函数
Hannah荣获第六季完美童模全球总决赛全球人气总冠军
新版本 MaxCompute 的SQL 中支持的 EXTRACT 函数有什么作用?
leetcode:187. 重复的DNA序列
[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 02
【无标题】
详谈RDMA技术原理和三种实现方式