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)

见后续文章

相关推荐
weifont1 小时前
聊一聊Electron中Chromium多进程架构
javascript·架构·electron
大得3691 小时前
electron结合vue,直接访问静态文件如何跳转访问路径
javascript·vue.js·electron
水银嘻嘻3 小时前
12 web 自动化之基于关键字+数据驱动-反射自动化框架搭建
运维·前端·自动化
it_remember3 小时前
新建一个reactnative 0.72.0的项目
javascript·react native·react.js
小嘟嚷ovo4 小时前
h5,原生html,echarts关系网实现
前端·html·echarts
十一吖i4 小时前
Vue3项目使用ElDrawer后select方法不生效
前端
只可远观4 小时前
Flutter目录结构介绍、入口、Widget、Center组件、Text组件、MaterialApp组件、Scaffold组件
前端·flutter
周胡杰4 小时前
组件导航 (HMRouter)+flutter项目搭建-混合开发+分栏效果
前端·flutter·华为·harmonyos·鸿蒙·鸿蒙系统
敲代码的小吉米4 小时前
前端上传el-upload、原生input本地文件pdf格式(纯前端预览本地文件不走后端接口)
前端·javascript·pdf·状态模式
是千千千熠啊4 小时前
vue使用Fabric和pdfjs完成合同签章及批注
前端·vue.js