網(wǎng)站首頁 編程語言 正文
目錄
一 組件通訊介紹
二 函數(shù)組件通訊
三 類組件通訊??
一 組件通訊介紹
組件是獨立且封閉的單元,默認情況下,只能使用組件自己的數(shù)據(jù).
為了實現(xiàn)多個組件之間共享數(shù)據(jù),就需要打破組件的獨立封閉性,讓組件與外界溝通,這個過程就是組件通訊.
組件是封閉的,要接收外部數(shù)據(jù)通過props來實現(xiàn).
二 函數(shù)組件通訊
使用步驟
1 傳遞數(shù)據(jù):給組件標簽添加屬性
2 接收數(shù)據(jù):通過props接收數(shù)據(jù)
說明:<Hello>組件有2個屬性 name和age ,這兩個屬性會組成一個對象props來進行傳遞
import React from "react";
import ReactDOM from "react-dom";
//2 接收數(shù)據(jù)
//props是一個對象
const Hello = props => {
console.log(props)
return (
<div>
<h1>name:{props.name}</h1>
<h1>age:{props.age}</h1>
</div>
)
}
//1 傳遞數(shù)據(jù)
ReactDOM.render(<Hello name='妹妹' age='3'/>, document.getElementById("root"));
備注:可以把參數(shù)的名字修改成props以外的名字
效果
控制臺
三 類組件通訊??
使用步驟
1 傳遞數(shù)據(jù):給組件標簽添加屬性
2 接收數(shù)據(jù):通過this.props接收數(shù)據(jù)
import React from "react";
import ReactDOM from "react-dom";
class Hello extends React.Component{
render(){
return (
<div>
{/*2 接收數(shù)據(jù)*/}
<h1>name:{this.props.name}</h1>
<h1>age:{this.props.age}</h1>
</div>
)
}
}
//1 傳遞數(shù)據(jù)
ReactDOM.render(<Hello name='妹妹' age='3'/>, document.getElementById("root"));
備注:this.props 不能改變名字
效果
?? ?
原文鏈接:https://blog.csdn.net/m0_45877477/article/details/125834340
- 上一篇:react props的特點
- 下一篇:react表單處理之 非受控組件
相關(guān)推薦
- 2022-07-12 element表格循環(huán)校驗,確保數(shù)據(jù)不重復
- 2022-09-23 Python線程threading(Thread類)_python
- 2022-05-12 linq中的串聯(lián)操作符_實用技巧
- 2022-12-14 詳解如何利用Redis實現(xiàn)生成唯一ID_Redis
- 2022-08-02 python機器學習Logistic回歸原理推導_python
- 2022-09-23 Pandas中Apply函數(shù)加速百倍的技巧分享_python
- 2022-09-01 Android使用Intent傳遞組件大數(shù)據(jù)_Android
- 2022-04-15 利用Python實現(xiàn)數(shù)值積分的方法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支