React-组件通信

1、父子组件通信

(1)父传子(props 传值)

javascript 复制代码
// 父组件
function App() {
  const name = '张三'
  return (
    <div className="App">
      <Son name={name} />
    </div>
  );
}
 
// 子组件
function Son(props) {
  return (
    <div>{ props.name }</div>
  )
}

(2)子传父(回调函数)

javascript 复制代码
// 父组件
function App() {
  const [msg, setMsg] = useState('');
  const getMsg = (data) => {
    console.log(data)
    setMsg(data)
  }
  return (
    <div className="App">
      <div>{ msg }</div>
      <Son onGetMsg={getMsg} />
    </div>
  );
}
 
// 子组件
function Son({ onGetMsg }) {
  return (
    <div>
      <button onClick={() => onGetMsg('子组件给父组件的数据')}>发送数据</button>
    </div>
  )
}

(3)双向数据绑定

javascript 复制代码
// 父组件
function App() {
  const [msg, setMsg] = useState('');
  const getMsg = (data) => {
    console.log(data);
    setMsg(data)
  }
  return (
    <div className="App">
      <div>{ msg }</div>
      <Son msg={msg} onGetMsg={getMsg} />
    </div>
  );
}
 
// 子组件
function Son({ msg, onGetMsg }) {
  return (
    <div>
      <input type="text" value={msg} onChange={(e) => onGetMsg(e.target.value)} />
    </div>
  )
}

2、兄弟组件通信

通过状态提升实现兄弟组件之间传值,子传父 => 父传子

javascript 复制代码
// 父组件
function App() {
  const [msg, setMsg] = useState('');
  const getMsg = (data) => {
    setMsg(data)
  }
  return (
    <div className="App">
      this is App
      <A onGetMsg={getMsg} />
      <B msg={msg} />
    </div>
  );
}

// 子组件A
function A({ onGetMsg }) {
  return (
    <div>
      this is A component
      <button onClick={() => onGetMsg('AAA')}>send</button>
    </div>
  )
}

// 子组件B
function B(props) {
  return (
    <div>
      this is B component
      { props.msg }
    </div>
  )
}

3、跨层级组件通信

  1. 使用createContext创建上下文对象
  2. 在顶层组件(App)中使用Ctx.Provider组件提供数据
  3. 在底层组件(B)中通过useContext获取消费数据
javascript 复制代码
import { createContext, useContext } from 'react';

// 1.使用createContext创建上下文对象
const MsgContent = createContext();

// 父组件
function App() {
  const msg = '哈哈哈';
  return (
    <div className="App">
      {/* 2.在顶层组件(App)中使用Ctx.Provider组件提供数据 */}
      <MsgContent.Provider value={msg}>
        this is App
        <A />
      </MsgContent.Provider>
    </div>
  );
}

// 子组件A
function A() {
  return (
    <div>
      this is A component
      <B />
    </div>
  )
}

// 孙子组件B
function B() {
  // 3.在底层组件(B)中通过useContext获取消费数据
  const msg = useContext(MsgContent);
  return (
    <div>
      this is B component
      {msg}
    </div>
  )
}

4、任意组件通信(Redux/Zustand)

见后续文章

相关推荐
_揽2 分钟前
html如何在一张图片上的某一个区域做到点击事件
前端·html
踢足球的,程序猿5 分钟前
从 Vue 2.0 进阶到 Vue 3.0 的核心技术解析指南
前端·javascript·vue.js·前端框架·html
冷凌爱7 分钟前
Fetch与Axios:区别、联系、优缺点及使用差异
前端·node.js·js
袁煦丞28 分钟前
跨平台终端王者Tabby:cpolar内网穿透实验室第632个成功挑战
前端·程序员·远程工作
Sailing30 分钟前
Grafana-mcp-analyzer:基于 MCP 的轻量 AI 分析监控图表的运维神器!
前端·node.js·mcp
阿山同学.1 小时前
AWS 亚马逊 S3存储桶直传 前端demo 复制即可使用
前端·javascript·aws
Jolyne_1 小时前
grid 实现完美的水平铺满、间隔一致的自适应布局
前端·css
sunly_1 小时前
Flutter:导航固定背景图,滚动时导航颜色渐变
android·javascript·flutter
西洼工作室1 小时前
【解决导航栏字体图标渲染导致文本闪烁问题】采用腾讯视频的解决方案
前端·css·css3
摸鱼仙人~1 小时前
深入理解Java单例模式:确保类只有一个实例
java·javascript·单例模式