做网站目录,百度竞价品牌广告,建立网站第一步怎么做,小程序开发公司网React组件通信方式总结#xff1a;1#xff09;父传子通过props单向传递数据#xff0c;子组件不能直接修改#xff1b;2#xff09;子传父通过回调函数实现数据传递#xff1b;3#xff09;兄弟组件通过状态提升到共同父组件实现通信#xff1b;4#xff09;跨层组件…React组件通信方式总结1父传子通过props单向传递数据子组件不能直接修改2子传父通过回调函数实现数据传递3兄弟组件通过状态提升到共同父组件实现通信4跨层组件可使用Context机制通过Provider提供数据useContext获取5复杂应用推荐Redux集中状态管理通过createSlice创建reducerProvider注入storeuseSelector和useDispatch操作状态。Redux Toolkit简化了Redux使用流程支持同步/异步操作。父传子父组件通过 props 传递数据给子组件子组件通过 props 接收数据。props 可以传递任意类型的数据包括字符串、布尔值、函数、对象、数组、JSX 等。// 父组件functionParent(){const[value,setValue]useState(父组件传递的数据);return(divChild value{value}//div);}// 子组件functionChild(props:{value:string}){returndiv{props.value}/div;}props 是只读的不能直接修改父组件的数据只能由父组件修改。props children// 父组件 标签嵌套Sonspan我是span标签/span/Son;// 子组件 通过props.children渲染在页面中functionSon(props:{children:React.ReactNode}){return(divh1我是子组件/h1hr/{props.children}/div);}子传父子组件通过 props 传递数据给父组件父组件通过 props 接收数据并传递给子组件。// 父组件functionParent(){const[value,setValue]useState();consthandleChildChange(value:string){setValue(value);};return(divChild onChange{handleChildChange}/p{value}/p/div);}// 子组件functionChild(props:{onChange:(value:string)void}){const[value,setValue]useState();consthandleChange(e:React.ChangeEventHTMLInputElement){setValue(e.target.value);props.onChange(e.target.value);};return(divinput typetextvalue{value}onChange{handleChange}//div);}兄弟组件使用状态提升实现兄弟组件通信即通过共同的父组件传递// 父组件functionParent(){const[value,setValue]useState();consthandleChildChange(value:string){setValue(value);};return(divChild1 onChange{handleChildChange}/Child2 value{value}//div);}// 子组件1functionChild1(props:{onChange:(value:string)void}){const[value,setValue]useState();consthandleChange(e:React.ChangeEventHTMLInputElement){setValue(e.target.value);props.onChange(e.target.value);};return(divinput typetextvalue{value}onChange{handleChange}//div);}// 子组件2functionChild2(props:{value:string}){return(divp{props.value}/p/div);}跨层组件通信——Context 机制// 1. 创建ContextconstMyContextcreateContext();// 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(){constvalueuseContext(MyContext);return(divp{value}/p/div);}跨层组件通信——Redux 机制 (集中状态管理工具)// 1. 安装// Redux Toolkit是一套工具集用于简化 Redux 的书写方式// react-redux链接React和Redux的中间件npm i reduxjs/toolkit react-redux// 2. 创建slice store/modules/counterStore.jsimport{createSlice}fromreduxjs/toolkit;importaxiosfromaxios;constcounterSlicecreateSlice({name:counter,// 命名空间// 初始化状态initialState:{count:0},// 定义修改状态的方法 同步方法 支持直接修改reducers:{increment(state){state.count;},decrement(state){state.count--;},// 支持传递参数 传递的参数会作为action.payload payload:固定的属性名addToNum(state,action){state.countaction.payload},},});constchannelSlicecreateSlice({name:channel,initialState:{channelList:[]},reducers:{// 获取异步数据setChannel(state,action){state.channelListaction.payload}}})// 解构actioncCreater函数const{increment,decrement,addToNum}counterSlice.actions;const{setChannel}channelSlice.actions;// 发起异步请求constgetChannelList(){returnasync(dispatch){constresawaitaxios.get(http://localhost:3000/channel);dispatch(setChannel(res.data))}}constcountReducercounterSlice.reducer;constchannelReducerchannelSlice.reducer;// 导出方法export{increment,decrement,addToNum,getChannelList};exportdefaultcountReducer;exportdefaultchannelReducer;// 3. 创建store store/index.jsimport{configureStore}fromreduxjs/toolkit;importcounterReducerfrom./counterSlice;importchannelReducerfrom./channelSlice;conststoreconfigureStore({reducer:{counter:counterReducer,channel:channelReducer,},});exportdefaultstore;// 4. 在顶层组件中提供store App.jsimport{Provider}fromreact-redux;importstorefrom./store;createRoot(document.getElementById(root)).render(StrictModeProvider store{store}App//Provider/StrictMode);// 5. 在子组件中获取store中的数据 useSelector映射store中的数据 useDispatch分发actionimport{useSelector,useDispatch}fromreact-redux;import{increment,decrement,addToNum,getChannelList}from./counterSlice;functionCounter(){// 映射store中的数据constcountuseSelector((state)state.counter);constlistuseSelector((state)state.channel);constdispatchuseDispatch();// 触发请求useEffect((){dispatch(getChannelList())},[dispatch])return(divp{count}/pbutton onClick{()dispatch(increment())}/buttonbutton onClick{()dispatch(decrement())}-/button{/* 传参 */}button onClick{()dispatch(addToNum(10))}10/button{/* 数据列表 */}ul{list.map((item)(li key{item.id}{item.name}/li))}/ul/div);}