news 2026/6/15 4:03:10

React 组件通信

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
React 组件通信

React组件通信方式总结:1)父传子通过props单向传递数据,子组件不能直接修改;2)子传父通过回调函数实现数据传递;3)兄弟组件通过状态提升到共同父组件实现通信;4)跨层组件可使用Context机制,通过Provider提供数据,useContext获取;5)复杂应用推荐Redux集中状态管理,通过createSlice创建reducer,Provider注入store,useSelector和useDispatch操作状态。Redux Toolkit简化了Redux使用流程,支持同步/异步操作。

  1. 父传子
    父组件通过 props 传递数据给子组件,子组件通过 props 接收数据。
    props 可以传递任意类型的数据,包括字符串、布尔值、函数、对象、数组、JSX 等。
// 父组件functionParent(){const[value,setValue]=useState("父组件传递的数据");return(<div><Child value={value}/></div>);}// 子组件functionChild(props:{value:string}){return<div>{props.value}</div>;}

props 是只读的,不能直接修改,父组件的数据只能由父组件修改。

props children

// 父组件 标签嵌套<Son><span>我是span标签</span></Son>;// 子组件 通过props.children渲染在页面中functionSon(props:{children:React.ReactNode}){return(<div><h1>我是子组件</h1><hr/>{props.children}</div>);}
  1. 子传父

子组件通过 props 传递数据给父组件,父组件通过 props 接收数据,并传递给子组件。

// 父组件functionParent(){const[value,setValue]=useState("");consthandleChildChange=(value:string)=>{setValue(value);};return(<div><Child onChange={handleChildChange}/><p>{value}</p></div>);}// 子组件functionChild(props:{onChange:(value:string)=>void}){const[value,setValue]=useState("");consthandleChange=(e:React.ChangeEvent<HTMLInputElement>)=>{setValue(e.target.value);props.onChange(e.target.value);};return(<div><input type="text"value={value}onChange={handleChange}/></div>);}
  1. 兄弟组件

使用状态提升实现兄弟组件通信,即通过共同的父组件传递

// 父组件functionParent(){const[value,setValue]=useState("");consthandleChildChange=(value:string)=>{setValue(value);};return(<div><Child1 onChange={handleChildChange}/><Child2 value={value}/></div>);}// 子组件1functionChild1(props:{onChange:(value:string)=>void}){const[value,setValue]=useState("");consthandleChange=(e:React.ChangeEvent<HTMLInputElement>)=>{setValue(e.target.value);props.onChange(e.target.value);};return(<div><input type="text"value={value}onChange={handleChange}/></div>);}// 子组件2functionChild2(props:{value:string}){return(<div><p>{props.value}</p></div>);}
  1. 跨层组件通信——Context 机制
// 1. 创建ContextconstMyContext=createContext();// 2. 顶层组件通过Provider组件提供数据functionParent(){const[value,setValue]=useState("");consthandleChildChange=(value:string)=>{setValue(value);};return(<MyContext.Provider value={value}><Child1 onChange={handleChildChange}/><Child2/></MyContext.Provider>);}// 3. 子组件通过useContext钩子获取数据functionChild2(){constvalue=useContext(MyContext);return(<div><p>{value}</p></div>);}
  1. 跨层组件通信——Redux 机制 (集中状态管理工具)
// 1. 安装// Redux Toolkit:是一套工具集,用于简化 Redux 的书写方式// react-redux:链接React和Redux的中间件npm i @reduxjs/toolkit react-redux// 2. 创建slice store/modules/counterStore.jsimport{createSlice}from"@reduxjs/toolkit";importaxiosfrom"axios";constcounterSlice=createSlice({name:"counter",// 命名空间// 初始化状态initialState:{count:0},// 定义修改状态的方法 同步方法 支持直接修改reducers:{increment(state){state.count++;},decrement(state){state.count--;},// 支持传递参数 传递的参数会作为action.payload payload:固定的属性名addToNum(state,action){state.count+=action.payload},},});constchannelSlice=createSlice({name:"channel",initialState:{channelList:[]},reducers:{// 获取异步数据setChannel(state,action){state.channelList=action.payload}}})// 解构actioncCreater函数const{increment,decrement,addToNum}=counterSlice.actions;const{setChannel}=channelSlice.actions;// 发起异步请求constgetChannelList=()=>{returnasync(dispatch)=>{constres=awaitaxios.get("http://localhost:3000/channel");dispatch(setChannel(res.data))}}constcountReducer=counterSlice.reducer;constchannelReducer=channelSlice.reducer;// 导出方法export{increment,decrement,addToNum,getChannelList};exportdefaultcountReducer;exportdefaultchannelReducer;// 3. 创建store store/index.jsimport{configureStore}from"@reduxjs/toolkit";importcounterReducerfrom"./counterSlice";importchannelReducerfrom"./channelSlice";conststore=configureStore({reducer:{counter:counterReducer,channel:channelReducer,},});exportdefaultstore;// 4. 在顶层组件中提供store App.jsimport{Provider}from"react-redux";importstorefrom"./store";createRoot(document.getElementById("root")).render(<StrictMode><Provider store={store}><App/></Provider></StrictMode>);// 5. 在子组件中获取store中的数据 useSelector:映射store中的数据 useDispatch:分发actionimport{useSelector,useDispatch}from"react-redux";import{increment,decrement,addToNum,getChannelList}from"./counterSlice";functionCounter(){// 映射store中的数据constcount=useSelector((state)=>state.counter);constlist=useSelector((state)=>state.channel);constdispatch=useDispatch();// 触发请求useEffect(()=>{dispatch(getChannelList())},[dispatch])return(<div><p>{count}</p><button onClick={()=>dispatch(increment())}>+</button><button onClick={()=>dispatch(decrement())}>-</button>{/* 传参 */}<button onClick={()=>dispatch(addToNum(10))}>+10</button>{/* 数据列表 */}<ul>{list.map((item)=>(<li key={item.id}>{item.name}</li>))}</ul></div>);}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/15 14:19:50

IL-7家族细胞因子:免疫稳态调控与肿瘤治疗的双重角色

一、IL-7及其受体系统的生物学特征 白细胞介素-7&#xff08;IL-7&#xff09;是IL-2细胞因子超家族成员&#xff0c;为T淋巴细胞和B淋巴细胞发育所必需的关键因子。该细胞因子主要由骨髓基质细胞、胸腺上皮细胞及特定非造血细胞产生&#xff0c;通过自分泌与旁分泌方式在初级…

作者头像 李华
网站建设 2026/6/15 11:31:26

卡尔曼滤波技术在水产养殖环境监测中的智能应用

卡尔曼滤波技术在水产养殖环境监测中的智能应用 【免费下载链接】Kalman-and-Bayesian-Filters-in-Python Kalman Filter book using Jupyter Notebook. Focuses on building intuition and experience, not formal proofs. Includes Kalman filters,extended Kalman filters, …

作者头像 李华
网站建设 2026/6/14 15:46:06

GoCV实战指南:构建高效计算机视觉应用完整教程

GoCV实战指南&#xff1a;构建高效计算机视觉应用完整教程 【免费下载链接】gocv hybridgroup/gocv: 是一个基于 Go 语言的开源计算机视觉库&#xff0c;支持多种计算机视觉算法和工具。该项目提供了一个简单易用的计算机视觉库&#xff0c;可以方便地实现图像和视频处理算法&a…

作者头像 李华
网站建设 2026/6/14 15:24:49

【独家披露】资深工程师的VSCode Qiskit部署秘籍:稳定运行不踩雷

第一章&#xff1a;VSCode Qiskit部署环境概览在量子计算快速发展的背景下&#xff0c;Qiskit作为IBM推出的开源量子软件开发工具包&#xff0c;已成为研究人员和开发者的重要选择。结合Visual Studio Code&#xff08;VSCode&#xff09;这一轻量级但功能强大的代码编辑器&…

作者头像 李华
网站建设 2026/6/14 18:44:06

Betaflight 2025.12 终极指南:开源飞控固件的完整升级与优化方案

还在为飞控固件的性能瓶颈而烦恼&#xff1f;Betaflight 2025.12版本带来了革命性的改进&#xff01;作为开源飞控固件领域的标杆项目&#xff0c;此次升级在系统架构、通信协议和飞行性能等方面都实现了重大突破。本文将为你提供详细的升级指南和性能优化方案&#xff0c;让你…

作者头像 李华