欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

react 組件傳值的三種方法

 更新時(shí)間:2019年06月03日 10:31:46   作者:阿西瓜  
這篇文章主要介紹了react 組件傳值的三種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

整理 react 組件傳值 三種方式

父組件向子組件傳值(通過props傳值)

子組件:

  class Children extends Component{
    constructor(props){
      super(props);
    }
    render(){
      return(
        <div>這是:{this.props.name}</div> // 這是 父向子
      )
    }
  }

父組件:

  class App extends React.Component{
    render(){
      return(
        <div>
          <Children name="父向子"/>
        </div>
      )
    }
  }

父組件向子組件傳值(回調(diào)函數(shù))

子組件

  class Children extends Component{
    constructor(props){
      super(props);
    }
    handerClick(){
      this.props.changeColor('skyblue') // 執(zhí)行父組件的changeColor 并傳參 必須和父組件中的函數(shù)一模一樣
    }
    render(){
      return(
        <div>
          <div>父組件的背景色{this.props.bgcolor}</div> // 子組件接收父組件傳過來的值 bgcolor
          <button onClick={(e)=>{this.handerClick(e)}}>改變父組件背景</button> // 子組件執(zhí)行函數(shù)
        </div>
      )
    }
  }

父組件

  class Father extends Component{
    constructor(props){
      super(props)
      this.state = {
        bgcolor:'pink'
      }
    }
    bgChange(color){
      this.setState({
        bgcolor:color
      })
    }
    render(props){
      <div style={{background:this.state.bgcolor}}>
              // 給子組件傳遞的值 color 
        <Children bgcolor={this.state.bgcolor} changeColor={(color)=>{this.bgChange(color)}} /> 
                          // changeColor 子組件的參數(shù)=color 當(dāng)做形參
      </div>
    }
  }

兄弟組件傳值(子傳給父,父再傳給另一個(gè)子)

子組件1

  class Children1 extends Component{
    constructor(props){
      super(props);
    }
    handerClick(){
      this.props.changeChild2Color('skyblue') 
      // 改變兄弟組件的顏色 把changeChild2Color的參數(shù)傳給父
    }
    render(){
      return(
        <div>
          <div>children1</div>
          <button onClick={(e)=>{this.handerClick(e)}}>改變children2背景</button>
        </div>
      )
    }
  }

子組件2

  class Children2 extends Component{
    constructor(props){
      super(props);
    }
    render(){
      return(
        <div style={{background:this.props.bgcolor}}>
        // 從父元素獲取自己的背景色
          <div>children2 背景色 {this.props.bgcolor}</div>
          // children2 背景色 skyblue
        </div>
      )
    }
  }  

父組件

class Father extends Component{
  constructor(props){
    super(props)
    this.state = {
      child2bgcolor:'pink'
    }
  }
  onchild2bgChange(color){
    this.setState({
      child2bgcolor:color
    })
  }
  render(props){
    <div>
      <Children1 changeChild2Color={(color)=>{this.onchild2bgChange(color)}} />
      <Children2 bgcolor={this.state.child2bgcolor} />
    </div>
  }
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • React手稿之 React-Saga的詳解

    React手稿之 React-Saga的詳解

    這篇文章主要介紹了React手稿之 React-Saga的詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-11-11
  • React捕獲并處理異常的方式

    React捕獲并處理異常的方式

    這篇文章主要給大家介紹了React優(yōu)雅的捕獲并處理渲染異常方式,文章通過代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2023-11-11
  • react實(shí)現(xiàn)每隔60s刷新一次接口的示例代碼

    react實(shí)現(xiàn)每隔60s刷新一次接口的示例代碼

    本文主要介紹了react實(shí)現(xiàn)每隔60s刷新一次接口的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • react父組件更改props子組件無法刷新原因及解決方法

    react父組件更改props子組件無法刷新原因及解決方法

    使用過vue的朋友都知道,vue父組件更改props的值,子組件是會(huì)刷新的,而react就未必,今天通過一個(gè)例子給大家介紹react父組件更改props子組件無法刷新原因,需要的朋友可以參考下
    2022-09-09
  • React/Redux應(yīng)用使用Async/Await的方法

    React/Redux應(yīng)用使用Async/Await的方法

    本篇文章主要介紹了React/Redux應(yīng)用使用Async/Await的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • React項(xiàng)目中應(yīng)用TypeScript的實(shí)現(xiàn)

    React項(xiàng)目中應(yīng)用TypeScript的實(shí)現(xiàn)

    TypeScript通常都會(huì)依賴于框架,例如和vue、react 這些框架結(jié)合,本文就主要介紹了React項(xiàng)目中應(yīng)用TypeScript的實(shí)現(xiàn),分享給大家,具體如下:
    2021-09-09
  • react-dnd實(shí)現(xiàn)任意拖動(dòng)與互換位置

    react-dnd實(shí)現(xiàn)任意拖動(dòng)與互換位置

    這篇文章主要為大家詳細(xì)介紹了react-dnd實(shí)現(xiàn)任意拖動(dòng)與互換位置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • React 自動(dòng)聚焦字段使用詳解

    React 自動(dòng)聚焦字段使用詳解

    這篇文章主要為大家介紹了React 自動(dòng)聚焦字段使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 在React中編寫樣式的六種方式

    在React中編寫樣式的六種方式

    在React中,編寫樣式主要有以下幾種方式,內(nèi)聯(lián)樣式,外部樣式表,CSS Modules,Styled Components,Emotion和Radium這六種樣式,下面我將針對(duì)上面提到的6種方式給出詳細(xì)的代碼示例,需要的朋友可以參考下
    2024-01-01
  • 記錄一次完整的react hooks實(shí)踐

    記錄一次完整的react hooks實(shí)踐

    這篇文章主要介紹了記錄一次完整的react hooks實(shí)踐,通過一個(gè)簡單示例,介紹了react hooks,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03

最新評(píng)論