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

vector类

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

TiKV & TiFlash 加速复杂业务查询丨TiFlash 应用实践

使用Stream多年,collect还有这些“骚操作”?

使用.NET简单实现一个Redis的高性能克隆版(一)

To participate in sweepstakes, incoming new programmers magazine welfare!

DAYU200 OpenHarmony标准系统HDMI全屏显示
[redis] cache penetration and cache avalanche and cache breakdown solutions

元宇宙系列--Value creation in the metaverse

B站回应HR称核心用户是Loser;微博回应宕机原因;Go 1.19 正式发布|极客头条
随机推荐
元宇宙系列--Value creation in the metaverse
蒋松廷 荣获第六季完美童模全球总决赛 全球总冠军
从零开始搭建MySQL主从复制架构
关于oracle表空间在线碎片整理
node connection mongoose database process
C专家编程 第1章 C:穿越时空的迷雾 1.11 轻松一下---由编译器定义的Pragmas效果
C专家编程 第3章 分析C语言的声明 3.2 声明是如何形成的
Windows 事件查看器记录到 MYSQL
83. Remove Duplicates from Sorted List
产品-Axure9英文版,轮播图效果
protobuf 中数据编码规则
将 Windows 事件日志错误加载到 SQL 表中
C专家编程 第3章 分析C语言的声明 3.8 理解所有分析过程的代码段
可复现、开放科研、跨学科合作:数据驱动下的科研趋势及应用方案
中小微企业如何简单便捷、低成本实现数字化?360视觉云有妙招
想进阿里?先来搞懂一下分布式事务
How ArkUI adapter somehow the screen
leetcode:187. 重复的DNA序列
如何在 DataWorks 中 写SQL语句监控数据的变化到达一定的值 进行提示
使用.NET简单实现一个Redis的高性能克隆版(一)