網(wǎng)站首頁 編程語言 正文
1、父組件傳值子組件
在引用子組件的時候傳遞,相當(dāng)于一個屬性,例如:在子組件內(nèi)通過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>
)
}
完整示例
創(chuàng)建父組件 index.js
import React from 'react';
import ReactDOM from 'react-dom';
import User from './User';//引入子組件
//定義數(shù)據(jù)
const person = {
name: 'Tom',
age:20
}
ReactDOM.render(
//渲染子組件,并向子組件傳遞name,age屬性
<User name={person.name} age={person.age}></User>
, document.getElementById('root'));
創(chuàng)建子組件 User.js
import React from 'react';
class User extends React.Component{
render(){
return (
// 使用props屬性接收父組件傳遞過來的參數(shù)
<div>{this.props.name},{this.props.age}</div>
);
}
}
export default User;
在父組件中可以使用展開運(yùn)算符 ... 傳遞對象
index.js文件
ReactDOM.render(
//渲染子組件,并向子組件傳遞name,age屬性
<User {...person}></User>
, document.getElementById('root'));
User.js文件
render(){
return (
// 使用props屬性接收父組件傳遞過來的參數(shù)
<div>{this.props.name},{this.props.age}</div>
);
}
2、子組件傳值父組件
子組件通過調(diào)用父組件傳遞到子組件的方法向父組件傳遞消息的。
完整案例
子組件 Son.js 文件代碼示例:
import React from 'react';
?
class Son extends React.Component {
? ? //構(gòu)造方法
? ? constructor(){
? ? ? ? super();
? ? ? ? this.state = {
? ? ? ? ? ? inputValue:''
? ? ? ? }
? ? }
? ? //按鈕點(diǎn)擊事件
? ? 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)}>點(diǎn)擊獲取數(shù)據(jù)</button>
? ? ? ? ? ? </React.Fragment>
? ? ? ? );
? ? }
?
}
?
export default Son;
父組件 Parent.js 文件代碼示例:
import React from 'react';
import Son from './Son';
?
class Parent extends React.Component {
? ? //構(gòu)造方法
? ? constructor(){
? ? ? ? super();
? ? ? ? this.state = {
? ? ? ? ? ? mess: '' //初始化mess屬性
? ? ? ? }
? ? }
? ? //用于接收子組件的傳值方法,參數(shù)為子組件傳遞過來的值
? ? getDatas(msg){
? ? ? ? //把子組件傳遞過來的值賦給this.state中的屬性
? ? ? ? this.setState({
? ? ? ? ? ? mess: msg
? ? ? ? });
? ? }
?
? ? render(){
? ? ? ? return (
? ? ? ? ? ? <React.Fragment>
? ? ? ? ? ? ? ? {/* 渲染子組件,設(shè)置子組件訪問的方法,
? ? ? ? ? ? ? ? getdata屬性名為子組件中調(diào)用的父組件方法名 */}
? ? ? ? ? ? ? ? <Son getdata={this.getDatas.bind(this)}></Son>
? ? ? ? ? ? ? ? <div>展示數(shù)據(jù):{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、兄弟組件傳值
兄弟組件之間的傳值,是通過父組件做的中轉(zhuǎn) ,流程為:
組件A -- 傳值 --> 父組件 -- 傳值 --> 組件B
代碼示例:
創(chuàng)建 Acls.js 組件,用于提供數(shù)據(jù)
import React from 'react';
?
class Acls extends React.Component {
? ? //按鈕點(diǎn)擊事件,向父組件Pcls.js傳值
? ? handleClick(){
? ? ? ? this.props.data("hello...React...");
? ? }
?
? ? render(){
? ? ? ? return (
? ? ? ? ? ? <button onClick={this.handleClick.bind(this)}>Acls組件中獲取數(shù)據(jù)</button>
? ? ? ? );
? ? }
}
?
export default Acls;
創(chuàng)建父組件 Pcls.js 用于中轉(zhuǎn)數(shù)據(jù)
import React from 'react';
import Acls from './Acls';
import Bcls from './Bcls';
?
class Pcls extends React.Component {
? ? //構(gòu)造函數(shù)
? ? constructor(){
? ? ? ? super();
? ? ? ? this.state = {
? ? ? ? ? ? mess: ''
? ? ? ? }
? ? }
? ? //向子組件Acls.js提供的傳值方法,參數(shù)為獲取的子組件傳過來的值
? ? 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;
創(chuàng)建子組件 Bcls.js 用于展示從 Acls.js 組件中生成的數(shù)據(jù)
import React from 'react';
?
class Bcls extends React.Component {
?
? ? render(){
? ? ? ? return (
? ? ? ? ? ? <div>在Bcls組件中展示數(shù)據(jù):{this.props.mess}</div>
? ? ? ? );
? ? }
}
?
export default Bcls;
原文鏈接:https://blog.csdn.net/p445098355/article/details/104519363
相關(guān)推薦
- 2022-10-08 C#中LINQ的Select與SelectMany函數(shù)使用_C#教程
- 2023-04-26 C語言實(shí)現(xiàn)數(shù)組元素排序方法詳解_C 語言
- 2022-12-19 Pytorch相關(guān)知識介紹與應(yīng)用_python
- 2022-06-18 kubernetes(k8s)安裝metrics-server實(shí)現(xiàn)資源使用情況監(jiān)控方式詳解_云其它
- 2022-03-26 C++實(shí)現(xiàn)簡單猜數(shù)字小游戲_C 語言
- 2022-10-17 在?C#?中使用?Span<T>?和?Memory<T>?編寫高性能代碼的詳
- 2022-11-04 golang?cache帶索引超時緩存庫實(shí)戰(zhàn)示例_Golang
- 2022-12-09 C++印刷模板使用方法詳解_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(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被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支