這篇文章將為大家詳細(xì)講解有關(guān)使用react怎么在父組件與子組件之間值傳遞,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
子組件
import React, { Component } from 'react' export default class Item extends Component { constructor(props) { super(props) this.state = { prices: 0 } } handleChange(){ const prices =800; this.setState({ prices: price }) //用傳過(guò)來(lái)的changePrice屬性(props),是個(gè)函數(shù),呼叫它把price交給父組件中的函數(shù)去處理 this.props.changePrice(price) } render() { const { prices } = this.state; return () } }{prices}
父組件
import React, { Component } from 'react'; import Item from './Item' class App extends Component { constructor(props) { super(props) this.state = {price: 0} } //給子組件用來(lái)傳price用的方法 changePrice(price){ this.setState({price: price}) } render() { return (); } } export default App;{this.state.price}
關(guān)于使用react怎么在父組件與子組件之間值傳遞就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。