当前位置: 首页 > news >正文

React高级特性之context

在这里插入图片描述

例1:

createContext

// 跨组件通信Context引入createContext
import React, { createContext }  from 'react'// App传数据给组件C  App -- A -- C// 1. 创建Context对象 
const { Provider, Consumer } = createContext()function SonA () {return (<div>我是组件SonA<SonC /></div>)
}// function SonC () {
//   return (
//     <div>
//       我是组件SonC
//       <Consumer >
//           {value => <div>我是{value.name}, 今年{value.age}</div>}
//       </Consumer>
//     </div>
//   )
// }
class SonC extends React.Component{render () {return (<div>我是类子组件SonC<Consumer>{value => <div>我是{value.name}, 今年{value.age}</div>}</Consumer></div>)}
}class App extends React.Component{state = {message:'this is message 666',obj: {name: 'zm',age: 25}}render () {return (<div>我是组件App<Provider value={this.state.obj}><SonA /></Provider></div>)}
}export default App// 跨组件通信Context  例如 父传孙
// Context 提供了一个无需为每层组件手动添加 props,就能在组件树间进行数据传递的方法// 1- 创建Context对象 导出 Provider 和 Consumer对象 
//    const { Provider, Consumer } = createContext()
// 2- 使用Provider包裹上层组件提供数据 
//    <Provider value={this.state.message}>
//       {/* 根组件 */}
//    </Provider>
// 3- 需要用到数据的组件使用Consumer包裹获取数据
//    <Consumer >
//      {value => /* 基于 context 值进行渲染*/}
//    </Consumer>

例2:

import React from 'react'// 创建 Context 填入默认值(任何一个 js 变量)
const ThemeContext = React.createContext('light')// 底层组件 - 函数是组件
function ThemeLink (props) {// const theme = this.context // 会报错。函数式组件没有实例,即没有 this// 函数式组件可以使用 Consumerreturn <ThemeContext.Consumer>{ value => <p>link's theme is {value}</p> }</ThemeContext.Consumer>
}// 底层组件 - class 组件
class ThemedButton extends React.Component {// 指定 contextType 读取当前的 theme context。// static contextType = ThemeContext // 也可以用 ThemedButton.contextType = ThemeContextrender() {const theme = this.context // React 会往上找到最近的 theme Provider,然后使用它的值。return <div><p>button's theme is {theme}</p></div>}
}
ThemedButton.contextType = ThemeContext // 指定 contextType 读取当前的 theme context。// 中间的组件再也不必指明往下传递 theme 了。
function Toolbar(props) {return (<div><ThemedButton /><ThemeLink /></div>)
}class App extends React.Component {constructor(props) {super(props)this.state = {theme: 'light'}}render() {return <ThemeContext.Provider value={this.state.theme}><Toolbar /><hr/><button onClick={this.changeTheme}>change theme</button></ThemeContext.Provider>}changeTheme = () => {this.setState({theme: this.state.theme === 'light' ? 'dark' : 'light'})}
}export default App

例3:

效果
在这里插入图片描述

index.tsx
在这里插入图片描述
ToolBar.tsx
在这里插入图片描述
ThemeButton.tsx
在这里插入图片描述

例4:

useContext

import { createContext, useContext } from 'react'// 创建Context对象
const Context = createContext()function ComA() {  return (<div>AAAAA ------ <ComC/></div>)
}
function ComC() {// 底层组件通过useContext函数获取数据  const num = useContext(Context)  return (<div>CCCCCC ----- {num}</div>)
}
function App() {  return (// 顶层组件通过Provider 提供数据    <Context.Provider value={99999}>     <div>666 ------ <ComA/></div>   </Context.Provider> )
}export default App// 实现步骤
// 1. 使用createContext 创建Context对象
// 2. 在顶层组件通过Provider 提供数据
// 3. 在底层组件通过useContext函数获取数据
http://www.lryc.cn/news/193023.html

相关文章:

  • 【OS】操作系统课程笔记 第五章 并发性——互斥、同步和通信
  • RabbitMQ概述原理
  • 8.Covector Transformation Rules
  • RustDay04------Exercise[21-30]
  • OpenAI科学家谈GPT-4的潜力与挑战
  • Java电子病历编辑器项目源码 采用B/S(Browser/Server)架构
  • 使用 AWS DataSync 进行跨区域 AWS EFS 数据传输
  • 设计模式~解释器模式(Interpreter)-19
  • 对象混入的实现方式
  • Mac 远程 Ubuntu
  • 黑豹程序员-h5前端录音、播放
  • Leetcode622.设计循环队列
  • 二十二、【形状工具组】
  • 设计模式~迭代器模式(Iterator)-20
  • 亳州市的自然风光与旅游资源:欣赏安徽省中部的壮丽景色
  • windows安装nvm以及解决yarn问题
  • 【TA 挖坑04】薄膜干涉 镭射材质 matcap
  • OpenCV13-图像噪声:椒盐噪声和高斯噪声
  • 天堂2服务器基本设置
  • 如何解决网站被攻击的问题
  • python爬虫入门详细教程-采集云南招聘网数据保存为csv文件
  • 1.13.C++项目:仿muduo库实现并发服务器之TcpServer模块的设计
  • Spring(17) AopContext.currentProxy() 类内方法调用切入
  • 自己的类支持基于范围的for循环 (深入探索)
  • Multi Scale Supervised 3D U-Net for Kidney and Tumor Segmentation
  • 《操作系统真象还原》第一章 部署工作环境
  • SpringCloud-Config
  • 劣币驱良币的 pacing 之殇
  • Gin 中的 Session(会话控制)
  • ChatGPT AIGC 实现数据分析可视化三维空间展示效果