码迷,mamicode.com
首页 > 其他好文 > 详细

React父子组件之间的对话

时间:2017-08-01 22:59:49      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:引入   方式   blog   ref   组件   bsp   image   模块   out   

父组件中引入子组件button,通过ref将组件绑定到父组件上,如果是公用模块,可以直接绑定最外层layout层。

<Button
      cb={() => {console.log(‘todo something‘)}}
      text=‘下一步‘
      bgArray={[‘#CDCDCD‘, ‘#73B2E6‘, ‘#0677DA‘]}
      ref={(button) => {
          this.button = button;
      }}
></Button>

子组件中暴露外部调用方式,供外部来改变子组件状态。代码如下:

技术分享

 

 只需要在父组件中调用

this.button.setBg(xx);

就能实现父组件传达给子组件的状态更改。

 

React父子组件之间的对话

标签:引入   方式   blog   ref   组件   bsp   image   模块   out   

原文地址:http://www.cnblogs.com/mickybg/p/7271102.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!