React組件通信實現(xiàn)流程詳解
組件間的關(guān)系
- 父子組件
- 兄弟組件
- 祖孫組件
通信方式
- 通過 props 方式傳遞數(shù)據(jù)。
- Context 方式(一般用于祖孫組件通信)。
- 集中式狀態(tài)管理 Redux(一般用于很多組件間都要共享數(shù)據(jù)的情況下)。
父子組件通信
父子組件通信一般通過 props 方式傳遞數(shù)據(jù)。
父組件向子組件傳遞數(shù)據(jù):
父組件通過向子組件傳遞 props,子組件得到 props 后進(jìn)行相應(yīng)的處理。
// Parent.js
import React, { Component } from "react";
import Child from "./Child.js";
export default class Parent extends Component{
render(){
return(
<div>
// 父組件通過 props 向子組件傳遞參數(shù)
<Child title = "父組件向子組件通信" />
</div>
)
}
}
// Child.js
import React,{ Component } from "react";
export default class Child extends Component{
render(){
return(
// 子組件通過 this.props 接收父組件傳遞過來的參數(shù)
<div>{this.props.title}</div>
)
}
}子組件主動觸發(fā)父組件方法,向父組件傳遞數(shù)據(jù):
父組件將一個函數(shù)作為 props 傳遞給子組件,子組件調(diào)用該函數(shù),便可以向父組件通信。
// Parent.js
import React,{ Component } from "react";
import Child from "./Child.js";
export default class Parent extends Component{
handleChange(val){
console.log(val) // 我是子組件傳給父組件的值
}
render(){
return(
<div>
// 父組件通過 props 把方法傳遞給子組件
<Child handleChange={this.handleChange} />
</div>
)
}
}
// Child.js
import React,{ Component } from "react";
export default class Child extends Component{
handleClick(){
// 子組件接收并調(diào)用父組件傳遞過來的方法
this.props.handleChange(‘我是子組件傳給父組件的值')
}
render(){
return(
<button onClick={this.handleClick}>按鈕</button>
)
}
}
父組件主動觸發(fā)子組件方法,獲取子組件數(shù)據(jù):
// Parent.js
import React,{ Component } from "react";
import Child from "./Child.js";
export default class Parent extends Component{
childRef = React.createRef()
handleClick(val){
//父組件觸發(fā)子組件方法
this.childRef.current.handleChange()
}
render(){
return(
<div onClick={this.handleClick}>
// 父組件通過 props 把方法傳遞給子組件
<Child ref={this.childRef} />
</div>
)
}
}
// Child.js
import React,{ Component } from "react";
export default class Child extends Component{
handleChange(){
// 子組件執(zhí)行邏輯操作
...
// 子組件返回數(shù)據(jù),傳遞給父組件
// return {}
}
render(){
return(
<button onClick={this.handleChange}>按鈕</button>
)
}
}
到此這篇關(guān)于React組件通信實現(xiàn)流程詳解的文章就介紹到這了,更多相關(guān)React組件通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于React中setState同步或異步問題的理解
相信很多小伙伴們都一直在疑惑,setState 到底是同步還是異步。本文就詳細(xì)的介紹一下React中setState同步或異步問題,感興趣的可以了解一下2021-11-11
React tabIndex使非表單元素支持focus和blur事件
這篇文章主要為大家介紹了React使用tabIndex使非表單元素支持focus和blur事件實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
React Native中WebView與html雙向通信遇到的坑
這篇文章主要介紹了React Native中WebView與html雙向通信的一些問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
React語法中設(shè)置TS校驗規(guī)則的步驟詳解
這篇文章主要給大家介紹了React語法中如何設(shè)置TS校驗規(guī)則,文中有詳細(xì)的代碼示例供大家參考,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-10-10
使用React+ts實現(xiàn)無縫滾動的走馬燈詳細(xì)過程
這篇文章主要給大家介紹了關(guān)于使用React+ts實現(xiàn)無縫滾動的走馬燈詳細(xì)過程,文中給出了詳細(xì)的代碼示例以及圖文教程,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2023-08-08
解決React在安裝antd之后出現(xiàn)的Can''t resolve ''./locale''問題(推薦)
這篇文章主要介紹了解決React在安裝antd之后出現(xiàn)的Can't resolve './locale'問題,本文給大家分享解決方案,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
react源碼層分析協(xié)調(diào)與調(diào)度
本文主要介紹了深入理解React協(xié)調(diào)與調(diào)度(Scheduler)原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-10-10

