您好,登錄后才能下訂單哦!
這篇文章主要介紹了react父子組件指的是什么的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇react父子組件指的是什么文章都會有所收獲,下面我們一起來看看吧。
在react組件的相互調用中,把調用者稱為父組件,被調用者稱為子組件。父子組件間可以傳值:1、父組件向子組件傳值時,先將需要傳遞的值傳遞給子組件,然后在子組件中,使用props來接收父組件傳遞過來的值;2、子組件向父組件傳值時,需要通過觸發方法來傳遞給父組件。
本教程操作環境:Windows7系統、react18版、Dell G3電腦。
react組件就是自己定義的非html標簽,規定react組件首字母大寫
:
class App extends Component{ } <App />
組件的相互調用中,把調用者
稱為父組件,被調用者
稱為子組件:
import React from 'react'; import Children from './Children'; class Up extends React.Component { constructor(props){ super(props); this.state = { } } render(){ console.log("render"); return( <div> up <Children /> </div> ) } } export default Up;
import React from 'react'; class Children extends React.Component{ constructor(props){ super(props); this.state = { } } render(){ return ( <div> Children </div> ) } } export default Children;
父組件向子組件傳值使用props。父組件向子組件傳值時,先將需要傳遞的值傳遞給子組件,然后在子組件中,使用props來接收父組件傳遞過來的值。
父組件在調用子組件的時候定義一個屬性:
<Children msg="父組件傳值給子組件" />
這個值msg
會綁定在子組件的props
屬性上,子組件可以直接使用:
this.props.msg
父組件可以給組件傳值,傳方法,甚至可以把自己傳遞給子組件
import React from 'react'; import Children from './Children'; class Up extends React.Component { constructor(props){ super(props); this.state = { } } render(){ console.log("render"); return( <div> up <Children msg="父組件傳值給子組件" /> </div> ) } } export default Up;
import React from 'react'; class Children extends React.Component{ constructor(props){ super(props); this.state = { } } render(){ return ( <div> Children <br /> {this.props.msg} </div> ) } } export default Children;
import React from 'react'; import Children from './Children'; class Up extends React.Component { constructor(props){ super(props); this.state = { } } run = () => { console.log("父組件run方法"); } render(){ console.log("render"); return( <div> up <Children run={this.run} /> </div> ) } } export default Up;
import React from 'react'; class Children extends React.Component{ constructor(props){ super(props); this.state = { } } run = () => { this.props.run(); } render(){ return ( <div> Children <br /> <button onClick={this.run}>Run</button> </div> ) } } export default Children;
import React from 'react'; import Children from './Children'; class Up extends React.Component { constructor(props){ super(props); this.state = { } } run = () => { console.log("父組件run方法"); } render(){ console.log("render"); return( <div> up <Children msg={this}/> </div> ) } } export default Up;
import React from 'react'; class Children extends React.Component{ constructor(props){ super(props); this.state = { } } run = () => { console.log(this.props.msg); } render(){ return ( <div> Children <br /> <button onClick={this.run}>Run</button> </div> ) } } export default Children;
子組件向父組件傳值通過觸發方法來傳值
import React from 'react'; import Children from './Children'; class Up extends React.Component { constructor(props){ super(props); this.state = { } } getChildrenData = (data) => { console.log(data); } render(){ console.log("render"); return( <div> up <Children upFun={this.getChildrenData}/> </div> ) } } export default Up;
import React from 'react'; class Children extends React.Component{ constructor(props){ super(props); this.state = { } } render(){ return ( <div> Children <br /> <button onClick={() => {this.props.upFun("子組件數據")}}>Run</button> </div> ) } } export default Children;
import React from 'react'; import Children from './Children'; class Up extends React.Component { constructor(props){ super(props); this.state = { } } clickButton = () => { console.log(this.refs.children); } render(){ console.log("render"); return( <div> up <Children ref="children" msg="test"/> <button onClick={this.clickButton}>click</button> </div> ) } } export default Up; ``` ```js import React from 'react'; class Children extends React.Component{ constructor(props){ super(props); this.state = { title: "子組件" } } runChildren = () => { } render(){ return ( <div> Children <br /> </div> ) } } export default Children; ``` ![在這里插入圖片描述](https://img-blog.csdnimg.cn/20200722065137748.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xldGlhbnhm,size_16,color_FFFFFF,t_70)
關于“react父子組件指的是什么”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“react父子組件指的是什么”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。