網站首頁 編程語言 正文
目錄
一?父組件 → 子組件
二 子組件 → 父組件
三 兄弟組件通訊
一?父組件 → 子組件
父組件傳遞數據給子組件
步驟
1 父組件提供要傳遞的數據,寫在state里
2 給子組件標簽添加屬性,值為state中的數據
3 子組件中通過props接收父組件傳遞的數據
import React from "react";
import ReactDOM from "react-dom";
//父組件
class Parent extends React.Component {
//1 父組件提供要傳遞的數據,寫在state里
state = {
lastName: "洛"
}
render() {
return (
<h1>
父組件:
{/*2 給子組件標簽添加屬性,值為state中的數據*/}
<Child name={this.state.lastName}></Child>
</h1>
)
}
}
//子組件
class Child extends React.Component {
render() {
return (<p>
子組件:
{/*3 子組件中通過props接收父組件傳遞的數據*/}
<span>{this.props.name}</span>
</p>)
}
}
// const Child = (props) => {
// return (<p>
// 子組件:
// {/*3 子組件中通過props接收父組件傳遞的數據*/}
// <span>{props.name}</span>
// </p>)
// }
//1 傳遞數據
ReactDOM.render(<Parent/>, document.getElementById("root"));
效果
?二 子組件 → 父組件
思路:利用回調函數,父組件提供回調,子組件調用,將要傳遞的數據作為回調函數的參數
import React from "react";
import ReactDOM from "react-dom";
//父組件
class Parent extends React.Component {
state = {
parentMsg: ''
}
//回調函數.用來接收數據
//4 執行回調函數,在頁面alert
getChildMsg = (data) => {
alert("接收到子組件傳遞過來的數據:" + data)
this.setState({
parentMsg: data
}
)
}
render() {
return (
<h1>
父組件:{this.state.parentMsg}
{/*子組件的屬性是回調函數*/}
{/*3 回調函數getMsg(msg) → getChildMsg(data)*/}
<Child getMsg={this.getChildMsg}></Child>
</h1>
)
}
}
//子組件
class Child extends React.Component {
//組件要傳遞給父組件的數據
state = {
msg: "我的帥爸爸"
}
handleClick = () => {
//2 子組件調用父組件中傳遞過來的回調函數getMsg(msg),回調函數的參數是 子組件要傳遞給父組件的數據
this.props.getMsg(this.state.msg)
}
render() {
return (<p>
子組件:
<div>
{/*1 點擊按鈕,走handleClick函數*/}
<button onClick={this.handleClick}>點我</button>
</div>
</p>)
}
}
ReactDOM.render(<Parent/>, document.getElementById("root"));
效果
?
?總結:
點擊按鈕 → 執行handleClick函數 → 執行getMsg(msg)? 該參數msg就是子組件要傳遞給父組件的數據 →?getChildMsg(data) → alert
三 兄弟組件通訊
將共享狀態提示到最近的公共父組件中,由公共父組件來管理這個狀態
思想:狀態提升
公共父組件職責:
1 提供共享狀態
2 提供操作共享狀態的方法
?
代碼
import React from "react";
import ReactDOM from "react-dom";
//父組件
class Parent extends React.Component {
//提供共享狀態
state = {
score: 0
}
//提供操作共享狀態的方法
addScore = () => {
this.setState({
score:this.state.score+1
}
)
}
render() {
return (
<h1>
<Child1 score={this.state.score}/>
<Child2 addScore={this.addScore}/>
</h1>
)
}
}
//子組件1
class Child1 extends React.Component {
render() {
return (<h1>分數:{this.props.score}</h1>)
}
}
//子組件2
class Child2 extends React.Component {
render() {
return (<button onClick={this.props.addScore}>點我加分</button>)
}
}
ReactDOM.render(<Parent/>, document.getElementById("root"));
效果
點擊按鈕, 增加分數
原文鏈接:https://blog.csdn.net/m0_45877477/article/details/125837267
相關推薦
- 2022-04-30 python?DataFrame中loc與iloc取數據的基本方法實例_python
- 2022-08-27 C#8.0中的模式匹配_C#教程
- 2022-11-24 PyTorch?Dataset與DataLoader使用超詳細講解_python
- 2022-01-29 win server 2008 web IIS部署asp.net程序后,CSS樣式錯亂不顯示問題
- 2022-05-27 python使用pandas進行量化回測_python
- 2022-04-24 C語言字符函數中的isalnum()和iscntrl()你都知道嗎_C 語言
- 2022-11-03 關于golang?test緩存問題_Golang
- 2022-06-17 C#中Abstract方法和Virtual方法的區別_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支