React-54:Context(组件间进行通信)

一种组件间通信方式, 常用于【祖组件】与【后代组件】间通信
在应用开发中一般不用context, 一般都它的封装react插件

创建一个 上下文 content 上下文是一个独立于组件的对象 Provider 创建一个上下文 Consumer使用上下文

  1. 创建Context容器对象:
const XxxContext = React.createContext()  
  1. 渲染子组时,外面包裹xxxContext.Provider, 通过value属性给后代组件传递数据:
<xxxContext.Provider value={数据}>
		子组件
</xxxContext.Provider>
  1. 后代组件读取数据:

(1) 第一种方式:仅适用于类组件

 static contextType = xxxContext  // 声明接收context
 this.context // 读取context中的value数据

(2)第二种方式: 函数组件与类组件都可以

 <xxxContext.Consumer>
	    {
	      value => ( // value就是context中的value数据
	        要显示的内容
	      )
	    }
</xxxContext.Consumer>

全部代码:

import React from 'react';
// 创建一个 上下文 content 上下文是一个独立于组件的对象 Provider 创建一个上下文 Consumer使用上下文
const MyContext = React.createContext();
const { Provider, Consumer } = MyContext;
export default function Content() {
  const [userName, setUserName] = React.useState({ name: 'jack', age: 18 });
  return (
    <div>
      <h1>我是A组件</h1>
      <h2>我的用户名是{userName.name}</h2>
      <Provider value={userName}>
        <B userName={userName} />
      </Provider>
    </div>
  );
}

function B(props) {
  return (
    <div>
      <h3>我是B组件</h3>
      <h4>我的用户名是{props.userName.name}</h4>
      <C />
    </div>
  );
}

function C(props) {
  console.log(props);
  return (
    <div>
      <h3>我是C组件</h3>
      <h4>我的用户名是:</h4>
      {/* 接受context */}
      <Consumer>{(value) => `${value.name},年龄是${value.age}`}</Consumer>
    </div>
  );
}

---------------------------------------------------------------------------------- or

import React, { Component } from 'react'
import './index.css'

//创建Context对象
const MyContext = React.createContext()
const {Provider,Consumer} = MyContext
export default class A extends Component {

	state = {username:'tom',age:18}

	render() {
		const {username,age} = this.state
		return (
			<div className="parent">
				<h3>我是A组件</h3>
				<h4>我的用户名是:{username}</h4>
				<Provider value={{username,age}}>
					<B/>
				</Provider>
			</div>
		)
	}
}

class B extends Component {
	render() {
		return (
			<div className="child">
				<h3>我是B组件</h3>
				<C/>
			</div>
		)
	}
}

/* class C extends Component {
	//声明接收context
	static contextType = MyContext
	render() {
		const {username,age} = this.context
		return (
			<div className="grand">
				<h3>我是C组件</h3>
				<h4>我从A组件接收到的用户名:{username},年龄是{age}</h4>
			</div>
		)
	}
} */

function C(){
	return (
		<div className="grand">
			<h3>我是C组件</h3>
			<h4>我从A组件接收到的用户名:
			<Consumer>
				{value => `${value.username},年龄是${value.age}`}
			</Consumer>
			</h4>
		</div>
	)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

臧小川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值