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

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;

相关文章:

  • Conda 生态系统介绍
  • ARM CM3核 压栈流程
  • 同为科技智能PDU在数据中心场景的应用与解决方案
  • Redis-限流方案
  • GStreamer —— 2.13、Windows下Qt加载GStreamer库后运行 - “教程13:播放控制“(附:完整源码)
  • Unity摄像机跟随物体
  • 冒泡排序的算法实现
  • 基于vue框架的在线考试系统s581n(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 构建一个支持精度、范围和负数的-Vue-数字输入框
  • Vue2-3 优雅的在子组件修改父组件传递过来的v-model
  • 大语言模型从理论到实践(第二版)-学习笔记(绪论)
  • 正则表达式简述
  • BP神经网络终极进化:2025量子增强版Python实现(附元宇宙金融实战)
  • 2025年03月07日Github流行趋势
  • STM32 子设备通过CAN发送数据到主设备
  • git 添加额外的远程仓库 URL
  • 【每日学点HarmonyOS Next知识】Web跨域资源、Web长按菜单、Web拦截请求、禁止录屏、Base64图片宽高
  • WHAT - 前端阻塞场景梳理
  • Hive-优化(语法优化篇)
  • 【Unity】 HTFramework框架(六十一)Project窗口文件夹锁定器
  • 做调查哪个网站比较可靠/seo的定义
  • 做网站外包哪家好/传统营销和网络营销的区别
  • 石家庄网站制作机构/seoshanghai net
  • 网站建设怎样上传程序/关键词竞价排名名词解释
  • 来个网站急急急2021年/厦门人才网官方网站
  • 重庆医院门户网站建设/谷歌搜索网页版入口