網站首頁 編程語言 正文
1、父組件傳值子組件
在引用子組件的時候傳遞,相當于一個屬性,例如:在子組件內通過porps.param獲取到這個param的值。
父組件向子組件傳值,通過props,將父組件的state傳遞給了子組件。
父組件代碼片段:
constructor(props){
super(props)
this.state={
message:"i am from parent"
}
}
render(){
return(
<Child txt={this.state.message}/>
)
}
}
子組件代碼片段:
render(){
return(
<p>{this.props.txt}</p>
)
}
完整示例
創建父組件 index.js
import React from 'react';
import ReactDOM from 'react-dom';
import User from './User';//引入子組件
//定義數據
const person = {
name: 'Tom',
age:20
}
ReactDOM.render(
//渲染子組件,并向子組件傳遞name,age屬性
<User name={person.name} age={person.age}></User>
, document.getElementById('root'));
創建子組件 User.js
import React from 'react';
class User extends React.Component{
render(){
return (
// 使用props屬性接收父組件傳遞過來的參數
<div>{this.props.name},{this.props.age}</div>
);
}
}
export default User;
在父組件中可以使用展開運算符 ... 傳遞對象
index.js文件
ReactDOM.render(
//渲染子組件,并向子組件傳遞name,age屬性
<User {...person}></User>
, document.getElementById('root'));
User.js文件
render(){
return (
// 使用props屬性接收父組件傳遞過來的參數
<div>{this.props.name},{this.props.age}</div>
);
}
2、子組件傳值父組件
子組件通過調用父組件傳遞到子組件的方法向父組件傳遞消息的。
完整案例
子組件 Son.js 文件代碼示例:
import React from 'react';
?
class Son extends React.Component {
? ? //構造方法
? ? constructor(){
? ? ? ? super();
? ? ? ? this.state = {
? ? ? ? ? ? inputValue:''
? ? ? ? }
? ? }
? ? //按鈕點擊事件
? ? handleClick(){
? ? ? ? //通過props屬性獲取父組件的getdata方法,并將this.state值傳遞過去
? ? ? ? this.props.getdata(this.state.inputValue);
? ? }
? ? //輸入框事件,用于為this.state賦值
? ? handleChange(e){
? ? ? ? this.setState({
? ? ? ? ? ? inputValue: e.target.value
? ? ? ? });
? ? }
?
? ? render(){
? ? ? ? return (
? ? ? ? ? ? <React.Fragment>
? ? ? ? ? ? ? ? <input onChange={this.handleChange.bind(this)}></input>
? ? ? ? ? ? ? ? <button onClick={this.handleClick.bind(this)}>點擊獲取數據</button>
? ? ? ? ? ? </React.Fragment>
? ? ? ? );
? ? }
?
}
?
export default Son;
父組件 Parent.js 文件代碼示例:
import React from 'react';
import Son from './Son';
?
class Parent extends React.Component {
? ? //構造方法
? ? constructor(){
? ? ? ? super();
? ? ? ? this.state = {
? ? ? ? ? ? mess: '' //初始化mess屬性
? ? ? ? }
? ? }
? ? //用于接收子組件的傳值方法,參數為子組件傳遞過來的值
? ? getDatas(msg){
? ? ? ? //把子組件傳遞過來的值賦給this.state中的屬性
? ? ? ? this.setState({
? ? ? ? ? ? mess: msg
? ? ? ? });
? ? }
?
? ? render(){
? ? ? ? return (
? ? ? ? ? ? <React.Fragment>
? ? ? ? ? ? ? ? {/* 渲染子組件,設置子組件訪問的方法,
? ? ? ? ? ? ? ? getdata屬性名為子組件中調用的父組件方法名 */}
? ? ? ? ? ? ? ? <Son getdata={this.getDatas.bind(this)}></Son>
? ? ? ? ? ? ? ? <div>展示數據:{this.state.mess}</div>
? ? ? ? ? ? </React.Fragment>
? ? ? ? );
? ? }
?
}
?
export default Parent;
入口文件 index.js示例代碼:
import React from 'react';
import ReactDOM from 'react-dom';
import Parent from './Parent';
?
ReactDOM.render(<Parent></Parent>, document.getElementById('root'));
3、兄弟組件傳值
兄弟組件之間的傳值,是通過父組件做的中轉 ,流程為:
組件A -- 傳值 --> 父組件 -- 傳值 --> 組件B
代碼示例:
創建 Acls.js 組件,用于提供數據
import React from 'react';
?
class Acls extends React.Component {
? ? //按鈕點擊事件,向父組件Pcls.js傳值
? ? handleClick(){
? ? ? ? this.props.data("hello...React...");
? ? }
?
? ? render(){
? ? ? ? return (
? ? ? ? ? ? <button onClick={this.handleClick.bind(this)}>Acls組件中獲取數據</button>
? ? ? ? );
? ? }
}
?
export default Acls;
創建父組件 Pcls.js 用于中轉數據
import React from 'react';
import Acls from './Acls';
import Bcls from './Bcls';
?
class Pcls extends React.Component {
? ? //構造函數
? ? constructor(){
? ? ? ? super();
? ? ? ? this.state = {
? ? ? ? ? ? mess: ''
? ? ? ? }
? ? }
? ? //向子組件Acls.js提供的傳值方法,參數為獲取的子組件傳過來的值
? ? getDatas(data){
? ? ? ? this.setState({
? ? ? ? ? ? mess: data
? ? ? ? });
? ? }
?
? ? render(){
? ? ? ? return (
? ? ? ? ? ? <React.Fragment>
? ? ? ? ? ? ? ? Pcls組件中顯示按鈕并傳值:
? ? ? ? ? ? ? ? <Acls data={this.getDatas.bind(this)}></Acls>
? ? ? ? ? ? ? ? <Bcls mess={this.state.mess}></Bcls>
? ? ? ? ? ? </React.Fragment>
? ? ? ? );
? ? }
}
?
export default Pcls;
創建子組件 Bcls.js 用于展示從 Acls.js 組件中生成的數據
import React from 'react';
?
class Bcls extends React.Component {
?
? ? render(){
? ? ? ? return (
? ? ? ? ? ? <div>在Bcls組件中展示數據:{this.props.mess}</div>
? ? ? ? );
? ? }
}
?
export default Bcls;
原文鏈接:https://blog.csdn.net/p445098355/article/details/104519363
相關推薦
- 2022-05-28 C語言數據結構超詳細講解單向鏈表_C 語言
- 2023-01-18 你不知道的C++中namespace和using的用法實例_C 語言
- 2022-08-31 Centos安裝python3與scapy模塊的問題及解決方法_python
- 2022-10-21 golang?一次性定時器Timer用法及實現原理詳解_Golang
- 2023-01-08 Android?IntentFilter的匹配規則示例詳解_Android
- 2023-04-07 React?Fiber構建源碼解析_React
- 2022-02-27 jwt Claims token 秘鑰稍有不同也能解析成功 signWith setSigningK
- 2022-12-11 Flow如何解決背壓問題的方法詳解_Android
- 最近更新
-
- 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同步修改后的遠程分支