猿问

react里的this.props

请问下clickResult是调用什么函数取得的结果?this.props.onClick不是一个空函数吗?

慕村9548890
浏览 2041回答 2
2回答

饮歌长啸

你可以理解为回调函数,&nbsp;父组件调用子组件, 子组件在触发点击事件的时候, 会调用父组件的传入的回调函数.例:const fa = React.createClass({&nbsp; &nbsp; onFaClick(){&nbsp; &nbsp; &nbsp; &nbsp; alert("fa");&nbsp; &nbsp; },&nbsp; &nbsp; render(){&nbsp; &nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <son onClick={this.onFaClick}/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; }});const son = React.createClass({&nbsp; &nbsp; onClick(){&nbsp; &nbsp; &nbsp; &nbsp; alert("son");&nbsp; &nbsp; &nbsp; &nbsp; if(this.props.onClick){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.props.onClick();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; render(){&nbsp; &nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button onClick={this.onClick}></botton>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; }});
随时随地看视频慕课网APP
我要回答