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

React基础之组件通信

组件嵌套

父子传值实现

实现步骤

1.父组件传递数据-在子组件标签上绑定属性

2.子组件接收数据-子组件通过props参数接收数据

import React, { useRef, useState } from 'react';

//父传子

//1.父组件传递数据,需要在子组件标签上绑定数据

//2.子组件接收数据 props的参数

function Son(props){

  //props对象中包含了父组件中传递的所有数据

  console.log(props);

  return <div>this is Son {props.name},jsx:{props.child}</div>

}

function App() {

  const name='this is app'

  return (

    <div>

    <Son

    name={name}

    age={18}

    isTrue={false}

    list={['vue','React']}

    obj={{name:'jack'}}

    cb={()=>{console.log('111');}}

    child={<span>this is span</span>}

    ></Son>

    </div>

  );

}

export default App;

props的说明

props中可以传递任何数据

props是只读对象

子组件只能读取props中的数据,而不能直接修改父组件的数据

特殊的prop children

当我们把内容签到到子组件标签中,父组件回自动在名为children的props属性中接收该内容

import React, { useRef, useState } from 'react';

function Son(props){

  return <div>this is son ,{props.children}</div>

}

function App() {

  const name='this is app'

  return (

    <div>

    <Son>

      <span>this is span</span>

    </Son>

    </div>

  );

}

export default App;

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

子传父

在子组件中调用父组件中的函数并传递参数

import React, { useRef, useState } from 'react';

function Son({onGetSonMsg}){

  const sonMsg='this is son msg'

  return(

    <div>

      this is son<button onClick={()=>onGetSonMsg(sonMsg)}>{sonMsg}</button>

    </div>

  )

}

function App() {

 const [msg,setMsg]= useState('')

  const getmsg=(msg)=>{

    console.log(msg);

    setMsg(msg)

  }

  const name='this is app'

  return (

    <div>

      this is app,{msg}

    <Son

      onGetSonMsg={getmsg}

    >

    </Son>

    </div>

  );

}

export default App;

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

兄弟组件之间的数据传递

使用状态提升实现兄弟组件通信,A通过子传父将数据传入到父组件,然后在由父组件的父传子给子组件

import React, { useRef, useState } from 'react';

function A({onGetAName}){

  const name='this is A name'

  return(

    <div>

      this is A

      <button onClick={()=>onGetAName(name)}>send</button>

     

      </div>

 

  )

}

function B({name}){

  return(

    <div>this is B,

      {name}

    </div>

  )

}

function App() {

  const [name,setName]=useState('')

const getAname=(name)=>{

  console.log(name);

  setName(name)

}

  return (

    <div>

      this is App

      <A onGetAName={getAname}/>

      <B name={name}/>

    </div>

  );

}

export default App;

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

使用context机制跨层组件通信

1.使用createContext方法创建上下文对象Ctx

2.在顶层组件App中通过Ctx.Provider组件提供数据

3.在底层组件B中使用useContext钩子函数获取消费数据

import React, { createContext, useContext, useRef, useState } from 'react';

const MsgContext=createContext()

function A(){

  return(

    <div>

      this is App

      <B/>

    </div>

  )

}

function B(){

  const msg=useContext(MsgContext)

  return(

    <div>

      this is B,{msg}

    </div>

  )

}

function App() {

  const msg='this is app msg'

  return (

    <div>

      <MsgContext.Provider value={msg}>

         this is App

        <A/>

      </MsgContext.Provider>

     

    </div>

  );

}

export default App;


http://www.kler.cn/a/576917.html

相关文章:

  • ‌HTTP/1.0、HTTP/2.0和HTTP/3.0的区别
  • 网络基础(一)【网络发展/认识协议/网络 VS 系统/以太网通信原理/重谈协议/网络中的地址管理】
  • 番外篇 - Docker的使用
  • PyTorch深度学习框架60天进阶学习计划第14天:循环神经网络进阶
  • Flink SQL 读取 Kafka 数据到 Mysql 实战
  • 常见排序算法鉴赏(原理剖析+动图演示)
  • DeepSeek 与 ChatGPT的主要区别
  • 揭开AI-OPS 的神秘面纱 第三讲(上)数据平台层技术架构与组件选型分析
  • 2025网络安全漏洞
  • Spring Boot 项目中慢SQL优化方案
  • 【推荐项目】039-酒店预定系统
  • 如何在Android中实现网络请求
  • Linux权限维持之协议后门(七)
  • 问题解决:Kali Linux 中配置启用 Vim 复制粘贴功能
  • 【leetcode hot 100 141】环形链表
  • Chrome 中清理缓存的方法
  • C/C++基础知识复习(53)
  • ChatGPT4.5详细介绍和API调用详细教程
  • 【原创】springboot+vue城市公交网系统设计与实现
  • 无约束优化问题的求解