詳解React 父組件和子組件的數(shù)據(jù)傳輸
在學(xué)習(xí) React 框架組件間數(shù)據(jù)傳輸知識點前,我們需要先明確幾點使用原則。
- React的組件間通訊是單向的。數(shù)據(jù)必須是由父級傳到子級或者子級傳遞給父級層層傳遞。
- 如果要給兄弟級的組件傳遞數(shù)據(jù),那么就要先傳遞給公共的父級而后在傳遞給你要傳遞到的組件位置。
- 這種非父子關(guān)系的組件間傳遞數(shù)據(jù),不推薦使用這種層層傳遞的方式;而是選擇使用維護(hù)全局狀態(tài)功能模塊(Redux)
一、父組件向子組件傳遞數(shù)據(jù)
父組件向子組件傳遞數(shù)據(jù)是通過在父組件中引用子組件時,在子組件標(biāo)簽設(shè)置傳輸數(shù)據(jù)的屬性;而子組件中通過 this.props 接受傳過來的數(shù)據(jù);這樣就實現(xiàn)了父組件向子組件的數(shù)據(jù)傳輸。
1.1、父組件代碼
import React, { Component } from 'react';
import './App.css';
import Child from './child'
class App extends Component {
constructor(props){
super(props);
this.state={
msg:'父類的消息',
name:'John',
age:99
}
}
callback=(msg,name,age)=>{
// setState方法,修改msg的值,值是由child里面?zhèn)鬟^來的
this.setState({msg});
this.setState({name});
this.setState({age});
}
render() {
return (
<div className="App">
<p> Message: {this.state.msg}</p>
<Child callback={this.callback} age={this.state.age}
name={this.state.name}></Child>
</div>
);
}
}
export default App;
代碼說明:父組件在使用子組件(Child)的過程中,對子組件傳輸了兩個屬性(age和name)和一個方法(callback 先不考慮)。
關(guān)鍵代碼:
<Child name={this.state.name} age={this.state.age}></Child>
1.2、子組件代碼
import React from "react";
class Child extends React.Component{
constructor(props){
super(props);
this.state={
name:'Andy',
age:31,
msg:"來自子類的消息"
}
}
change=()=>{
this.props.callback(this.state.msg,this.state.name,this.state.age);
}
render(){
return(
<div>
<div>{this.props.name}</div>
<div>{this.props.age}</div>
<button onClick={this.change}>點擊</button>
</div>
)
}
}
export default Child;
代碼說明:子組件中在 render 中直接使用 this.props 接受父組件傳輸?shù)臄?shù)據(jù),并直接使用。不推薦子組件將接受到的數(shù)據(jù),再使用this.setSate 方式處理。
關(guān)鍵代碼:
<div>{this.props.name}</div>
<div>{this.props.age}</div>
二、子組件向父組件傳輸數(shù)據(jù)
React 框架中子組件向父組件傳輸數(shù)據(jù),要依賴于父組件向子組件傳輸數(shù)據(jù)。實際上就是父組件將自己作用域的函數(shù)傳輸給子組件;子組件調(diào)用該函數(shù),并將要傳輸?shù)臄?shù)據(jù),通過函數(shù)的參數(shù)的形式,傳輸給父組件。
2.1、父組件代碼
上面的代碼示例中,父組件中定義了函數(shù),并將這個函數(shù)傳輸給了子組件。
class App extends Component {
......
callback=(msg,name,age)=>{
// setState方法,修改msg的值,值是由child里面?zhèn)鬟^來的
this.setState({msg});
this.setState({name});
this.setState({age});
}
render() {
return (
<div className="App">
<Child callback={this.callback}></Child>
</div>
);
}
}
export default App;
父組件將自己作用域的函數(shù)傳遞給子組件,子組件在通過 this.props 調(diào)用此函數(shù)的過程中,通過參數(shù)的方式將數(shù)據(jù)傳輸?shù)浇M組件中。
這里父組件有三個形參:msg,name,age;子組件將數(shù)據(jù)傳輸過來后,父組件會將其使用 this.setState 方式處理。
2.2、子組件代碼
子組件通過使用 this.props 接受到父組件傳輸過來的函數(shù);并調(diào)用此函數(shù)通過參數(shù)的方法,傳輸數(shù)據(jù)給父組件。
class Child extends React.Component{
......
change=()=>{
this.props.callback(this.state.msg,this.state.name,this.state.age);
}
render(){
return(
<div>
<button onClick={this.change}>點擊</button>
</div>
)
}
}
export default Child;
子組件中創(chuàng)建了一個方法 change(),此方法和點擊事件 onClick 綁定;change() 方法中會調(diào)用 this.props.callback() 函數(shù)(父組件傳輸過來的函數(shù));函數(shù)的實參就是子組件傳輸給父組件的數(shù)據(jù)。
以上就是詳解React 父組件和子組件的數(shù)據(jù)傳輸?shù)脑敿?xì)內(nèi)容,更多關(guān)于React 父組件和子組件的數(shù)據(jù)傳輸?shù)馁Y料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
React Hook useState useEffect componentD
這篇文章主要介紹了React Hook useState useEffect componentDidMount componentDidUpdate componentWillUnmount問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
React中useEffect 與 useLayoutEffect的區(qū)別
本文主要介紹了React中useEffect與useLayoutEffect的區(qū)別,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
使用useMutation和React Query發(fā)布數(shù)據(jù)demo
這篇文章主要為大家介紹了使用useMutation和React Query發(fā)布數(shù)據(jù)demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
React創(chuàng)建組件的三種方式及其區(qū)別
本文主要介紹了React創(chuàng)建組件的三種方式及其區(qū)別,具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01

