React父子組件傳值(組件通信)的實(shí)現(xiàn)方法
1、父組件傳值子組件
在引用子組件的時(shí)候傳遞,相當(dāng)于一個(gè)屬性,例如:在子組件內(nèi)通過(guò)porps.param獲取到這個(gè)param的值。
父組件向子組件傳值,通過(guò)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屬性接收父組件傳遞過(guò)來(lái)的參數(shù)
<div>{this.props.name},{this.props.age}</div>
);
}
}
export default User;
在父組件中可以使用展開(kāi)運(yùn)算符 ... 傳遞對(duì)象
index.js文件
ReactDOM.render(
//渲染子組件,并向子組件傳遞name,age屬性
<User {...person}></User>
, document.getElementById('root'));
User.js文件
render(){
return (
// 使用props屬性接收父組件傳遞過(guò)來(lái)的參數(shù)
<div>{this.props.name},{this.props.age}</div>
);
}
2、子組件傳值父組件
子組件通過(guò)調(diào)用父組件傳遞到子組件的方法向父組件傳遞消息的。
完整案例
子組件 Son.js 文件代碼示例:
import React from 'react';
?
class Son extends React.Component {
? ? //構(gòu)造方法
? ? constructor(){
? ? ? ? super();
? ? ? ? this.state = {
? ? ? ? ? ? inputValue:''
? ? ? ? }
? ? }
? ? //按鈕點(diǎn)擊事件
? ? handleClick(){
? ? ? ? //通過(guò)props屬性獲取父組件的getdata方法,并將this.state值傳遞過(guò)去
? ? ? ? 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ù)為子組件傳遞過(guò)來(lái)的值
? ? getDatas(msg){
? ? ? ? //把子組件傳遞過(guò)來(lái)的值賦給this.state中的屬性
? ? ? ? this.setState({
? ? ? ? ? ? mess: msg
? ? ? ? });
? ? }
?
? ? render(){
? ? ? ? return (
? ? ? ? ? ? <React.Fragment>
? ? ? ? ? ? ? ? {/* 渲染子組件,設(shè)置子組件訪問(wèn)的方法,
? ? ? ? ? ? ? ? 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、兄弟組件傳值
兄弟組件之間的傳值,是通過(guò)父組件做的中轉(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ù)為獲取的子組件傳過(guò)來(lái)的值
? ? 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;到此這篇關(guān)于React父子組件傳值(組件通信)的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)React父子組件傳值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解React中傳入組件的props改變時(shí)更新組件的幾種實(shí)現(xiàn)方法
這篇文章主要介紹了詳解React中傳入組件的props改變時(shí)更新組件的幾種實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
適用于React?Native?旋轉(zhuǎn)木馬應(yīng)用程序介紹
這篇文章主要介紹了適用于React?Native?旋轉(zhuǎn)木馬應(yīng)用程序介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
React+ts實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了React+ts實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
React使用Ant Design方式(簡(jiǎn)單使用)
文章介紹了AntDesign組件庫(kù),它是基于AntDesign設(shè)計(jì)體系的ReactUI組件庫(kù),主要用于研發(fā)企業(yè)級(jí)中后臺(tái)產(chǎn)品,文章詳細(xì)講解了如何下載和按需引入antd組件庫(kù),并通過(guò)一個(gè)小案例展示了如何使用antd進(jìn)行布局和改造,最后,文章提醒大家在使用過(guò)程中可以參考官網(wǎng)的屬性介紹2024-11-11
基于Webpack5 Module Federation的業(yè)務(wù)解耦實(shí)踐示例
這篇文章主要為大家介紹了基于Webpack5 Module Federation的業(yè)務(wù)解耦實(shí)踐示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
詳解React Native 采用Fetch方式發(fā)送跨域POST請(qǐng)求
這篇文章主要介紹了詳解React Native 采用Fetch方式發(fā)送跨域POST請(qǐng)求,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
react 權(quán)限樹(shù)形結(jié)構(gòu)實(shí)現(xiàn)代碼
這篇文章主要介紹了react 權(quán)限樹(shù)形結(jié)構(gòu)實(shí)現(xiàn)代碼,項(xiàng)目背景react + ant design,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-05-05

