React組件通信實(shí)現(xiàn)方式詳解
1. 父子組件通信方式
?父子組件之間的通信很常見,其中父組件可以通過props,原型方法向子組件通信,同時(shí)子組件也可以用回調(diào)函數(shù),事件冒泡向父組件通信
父傳子
原型方法
父組件通過React.createRef()創(chuàng)建Ref,保存在實(shí)例屬性myRef上。父組件
中,渲染子組件時(shí),定義一個(gè)Ref屬性,值為剛創(chuàng)建的myRef。
父組件調(diào)用子組件的myFunc函數(shù),傳遞一個(gè)參數(shù),子組件接收到參數(shù),打印出參數(shù)。
this.props
name作為props由父組件傳遞給子組件,子組件拿到name后,渲染在頁面上。參數(shù)有父組件傳遞給子組件
import React, { Component, Fragment } from 'react'; class Son extends Component { myFunc(name) { console.log(name); } render() { return <></>; } } // 父組件 export default class Father extends Component { constructor(props) { super(props); // 創(chuàng)建Ref,并保存在實(shí)例屬性myRef上 this.myRef = React.createRef(); } componentDidMount() { // 調(diào)用子組件的函數(shù),傳遞一個(gè)參數(shù) this.myRef.current.myFunc('Jack'); } render() { return ( <> <Son ref={this.myRef} /> </> ); } }
子傳父
- 回調(diào)函數(shù)
- 事件冒泡
在子組件內(nèi)部,修改了父組件中的值,從而完成了子組件向父組件通信
import React, { Component } from 'react' class Navbar extends Component{ render(){ return <div style={{background:"red"}}> <button onClick={()=>{ console.log("子通知父, 讓父的isSHow 取反。",this.props.event) this.props.event() //調(diào)用父組件傳來啊的回調(diào)函數(shù) }}>click</button> <span>navbar</span> </div> } } class Sidebar extends Component{ render(){ return <div style={{background:"yellow",width:"200px"}}> <ul> <li>11111</li> </ul> </div> } } export default class App extends Component { state = { isShow:false } handleEvent = ()=>{ this.setState({ isShow:!this.state.isShow }) // console.log("父組件定義的event事件") } render() { return ( <div> <Navbar event={this.handleEvent}/> {/* <button onClick={()=>{ this.setState({ isShow:!this.state.isShow }) }}>click</button> */} {this.state.isShow && <Sidebar/>} </div> ) } } /* 父傳子 :屬性, 子傳父: 回調(diào)函數(shù) callback */
2. 非父子組件通信方式
??狀態(tài)提升
React中的狀態(tài)提升概括來說,就是將多個(gè)組件需要共享的狀態(tài)提升到它們最近的父組件上.在父組件上改變這個(gè)狀態(tài)然后通過props
??發(fā)布訂閱模式實(shí)現(xiàn)
??context狀態(tài)樹傳參
a. 先定義全局context對象 import React from 'react' const GlobalContext = React.createContext() export default GlobalContext
b. 根組件引入GlobalContext,并使用GlobalContext.Provider(生產(chǎn)者) //重新包裝根組件 class App {} <GlobalContext.Provider value={{ name:"kerwin", age:100, content:this.state.content, show:this.show.bind(this), hide:this.hide.bind(this) }} > <之前的根組件></之前的根組件> </GlobalContext.Provider>
c. 任意組件引入GlobalContext并調(diào)用context,使用GlobalContext.Consumer(消費(fèi)者) <GlobalContext.Consumer> { context => { this.myshow = context.show; //可以在當(dāng)前組件任意函數(shù)觸發(fā) this.myhide = context.hide;//可以在當(dāng)前組件任意函數(shù)觸發(fā) return ( <div> {context.name}-{context.age}-{context.content} </div> ) } } </GlobalContext.Consumer>
注意:GlobalContext.Consumer內(nèi)必須是回調(diào)函數(shù),通過context方法改變根組件狀態(tài)
context優(yōu)缺點(diǎn):
優(yōu)點(diǎn):跨組件訪問數(shù)據(jù)
缺點(diǎn):react組件樹種某個(gè)上級組件shouldComponetUpdate 返回false,當(dāng)context更新時(shí),不會(huì)引起下級組件更新
到此這篇關(guān)于React組件通信實(shí)現(xiàn)方式詳解的文章就介紹到這了,更多相關(guān)React組件通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一篇文章教你用React實(shí)現(xiàn)菜譜系統(tǒng)
本篇文章主要介紹了React實(shí)現(xiàn)菜譜軟件的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2021-09-09react-redux及redux狀態(tài)管理工具使用詳解
Redux是為javascript應(yīng)用程序提供一個(gè)狀態(tài)管理工具集中的管理react中多個(gè)組件的狀態(tài)redux是專門作狀態(tài)管理的js庫(不是react插件庫可以用在其他js框架中例如vue,但是基本用在react中),這篇文章主要介紹了react-redux及redux狀態(tài)管理工具使用詳解,需要的朋友可以參考下2023-01-01詳解react-router 4.0 下服務(wù)器如何配合BrowserRouter
這篇文章主要介紹了詳解react-router 4.0 下服務(wù)器如何配合BrowserRouter,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12漸進(jìn)式源碼解析React更新流程驅(qū)動(dòng)
這篇文章主要為大家介紹了漸進(jìn)式源碼解析React更新流程驅(qū)動(dòng)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04React中useCallback useMemo到底該怎么用
在React函數(shù)組件中,當(dāng)組件中的props發(fā)生變化時(shí),默認(rèn)情況下整個(gè)組件都會(huì)重新渲染。換句話說,如果組件中的任何值更新,整個(gè)組件將重新渲染,包括沒有更改values/props的函數(shù)/組件。在react中,我們可以通過memo,useMemo以及useCallback來防止子組件的rerender2023-02-02React使用Context與router實(shí)現(xiàn)權(quán)限路由詳細(xì)介紹
這篇文章主要介紹了React使用Context與router實(shí)現(xiàn)權(quán)限路由的詳細(xì)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01