当前位置:网站首页>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
边栏推荐
- error:Illegal instruction (core dumped),离线下载安装这个other版本numpy
- 从MatePad Pro进化看鸿蒙OS的生态势能
- Looking at the ecological potential of Hongmeng OS from the evolution of MatePad Pro
- #夏日挑战赛#【FFH】OpenHarmony设备开发基础(四)启动流程
- leetcode:187. 重复的DNA序列
- Excuse me this hologres dimension table is cached?How to Finished
- 使用.NET简单实现一个Redis的高性能克隆版(一)
- C专家编程 第3章 分析C语言的声明 3.5 typedef可以成为你的朋友
- 设置海思芯片MMZ内存、OS内存详解
- [Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 01
猜你喜欢

高效的组织信息共享知识库是一种宝贵的资源

使用uniapp 封装一个request 请求

世界顶级级架构师编写2580页DDD领域驱动设计笔记,属实有牌面
![[Unity Getting Started Plan] Basic Concepts (6) - Sprite Renderer Sprite Renderer](/img/c2/5717cb053bc6c07829a616735cbc71.png)
[Unity Getting Started Plan] Basic Concepts (6) - Sprite Renderer Sprite Renderer

2年开发经验去面试,吊打面试官,即将面试的程序员这些笔记建议复习

【带你了解SDN和网络虚拟化】
![[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 01](/img/8e/fcf79d150af4384c14a118fb209725.png)
[Unity Getting Started Plan] Basic Concepts (8) - Tile Map TileMap 01

组件通信-父传子组件通信

MySQL窗口函数

最强分布式锁工具:Redisson
随机推荐
Kubernetes 笔记 / 生产环境
产品-Axure9英文版,轮播图效果
关于oracle表空间在线碎片整理
如何选择合适的导电滑环型号
13 and OOM simulation
node connection mongoose database process
【无标题】
Kubernetes 笔记 / 目录
yolov5s用自己的数据集进行训练模型
leetcode-268.丢失的数字
Async的线程池使用的哪个?
C# 获取文件名和扩展名(后缀名)
C专家编程 第3章 分析C语言的声明 3.6 typedef int x[10]和#define x int[10]的区别
MATLAB | 一种简易的随机曼陀罗图形生成函数
leetcode:202. 快乐数
滑环安装注意事项
Auto Scaling 弹性伸缩(运维释放人力)
Windows 事件转发到 SQL 数据库
C专家编程 第2章 这不是Bug,而是语言特性 2.4 少做之过
视频人脸识别和图片人脸识别的关系