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í),不會引起下級組件更新
到此這篇關(guān)于React組件通信實(shí)現(xiàn)方式詳解的文章就介紹到這了,更多相關(guān)React組件通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react中如何對自己的組件使用setFieldsValue
react中如何對自己的組件使用setFieldsValue問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03React系列useSyncExternalStore學(xué)習(xí)詳解
這篇文章主要為大家介紹了React系列useSyncExternalStore的學(xué)習(xí)及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07React報(bào)錯(cuò)信息之Expected?an?assignment?or?function?call?and?
這篇文章主要介紹了React報(bào)錯(cuò)之Expected?an?assignment?or?function?call?and?instead?saw?an?expression,下面有兩個(gè)示例來展示錯(cuò)誤是如何產(chǎn)生的,需要的朋友可以參考下2022-08-08使用webpack5從0到1搭建一個(gè)react項(xiàng)目的實(shí)現(xiàn)步驟
這篇文章主要介紹了使用webpack5從0到1搭建一個(gè)react項(xiàng)目的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12React hook 'useState' is calle
這篇文章主要為大家介紹了React hook 'useState' is called conditionally報(bào)錯(cuò)解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12