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)

见后续文章

相关推荐
小希爸爸4 分钟前
2、中医基础入门和养生
前端·后端
局外人LZ8 分钟前
前端项目搭建集锦:vite、vue、react、antd、vant、ts、sass、eslint、prettier、浏览器扩展,开箱即用,附带项目搭建教程
前端·vue.js·react.js
G_GreenHand22 分钟前
Dhtmlx Gantt教程
前端
鹿九巫23 分钟前
【CSS】层叠,优先级与继承(四):层叠,优先级与继承的关系
前端·css
卓怡学长25 分钟前
w304基于HTML5的民谣网站的设计与实现
java·前端·数据库·spring boot·spring·html5
宝拉不想努力了28 分钟前
vue element使用el-table时,切换tab,table表格列项发生错位问题
前端·vue.js·elementui
YONG823_API32 分钟前
深度探究获取淘宝商品数据的途径|API接口|批量自动化采集商品数据
java·前端·自动化
鱼樱前端33 分钟前
前端必知必会:JavaScript 对象与数组克隆的 7 种姿势,从浅入深一网打尽!
前端·javascript
yzhSWJ1 小时前
Spring Boot中自定义404异常处理问题学习笔记
java·javascript
小希爸爸1 小时前
1、中医基础入门和养生
前端·后端