React組件通信實現(xiàn)方式詳解
1. 父子組件通信方式
?父子組件之間的通信很常見,其中父組件可以通過props,原型方法向子組件通信,同時子組件也可以用回調(diào)函數(shù),事件冒泡向父組件通信
父傳子
原型方法
父組件通過React.createRef()創(chuàng)建Ref,保存在實例屬性myRef上。父組件
中,渲染子組件時,定義一個Ref屬性,值為剛創(chuàng)建的myRef。
父組件調(diào)用子組件的myFunc函數(shù),傳遞一個參數(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,并保存在實例屬性myRef上
this.myRef = React.createRef();
}
componentDidMount() {
// 調(diào)用子組件的函數(shù),傳遞一個參數(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)提升概括來說,就是將多個組件需要共享的狀態(tài)提升到它們最近的父組件上.在父組件上改變這個狀態(tài)然后通過props
??發(fā)布訂閱模式實現(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(消費者)
<GlobalContext.Consumer>
{
context => {
this.myshow = context.show; //可以在當前組件任意函數(shù)觸發(fā)
this.myhide = context.hide;//可以在當前組件任意函數(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)缺點:
優(yōu)點:跨組件訪問數(shù)據(jù)
缺點:react組件樹種某個上級組件shouldComponetUpdate 返回false,當context更新時,不會引起下級組件更新
到此這篇關(guān)于React組件通信實現(xiàn)方式詳解的文章就介紹到這了,更多相關(guān)React組件通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一篇文章教你用React實現(xiàn)菜譜系統(tǒng)
本篇文章主要介紹了React實現(xiàn)菜譜軟件的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2021-09-09
react-redux及redux狀態(tài)管理工具使用詳解
Redux是為javascript應(yīng)用程序提供一個狀態(tài)管理工具集中的管理react中多個組件的狀態(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,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
React中useCallback useMemo到底該怎么用
在React函數(shù)組件中,當組件中的props發(fā)生變化時,默認情況下整個組件都會重新渲染。換句話說,如果組件中的任何值更新,整個組件將重新渲染,包括沒有更改values/props的函數(shù)/組件。在react中,我們可以通過memo,useMemo以及useCallback來防止子組件的rerender2023-02-02
React使用Context與router實現(xiàn)權(quán)限路由詳細介紹
這篇文章主要介紹了React使用Context與router實現(xiàn)權(quán)限路由的詳細過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-01-01

