react 自我小计
发布时间:2020-12-15 20:37:33 所属栏目:百科 来源:网络整理
导读:1、react中的方法调用,在onClick事件中不需要加小括号。 button onClick={ this .show}方法的调用/button show(){ console.log( ‘按钮的onclick事件触发‘ ); } 2、react中使用this.setState,在onClick事件中不需要加小括号,但是在方法上要用箭头函数。下
1、react中的方法调用,在onClick事件中不需要加小括号。 <button onClick={this.show}>方法的调用</button> show(){ console.log(‘按钮的onclick事件触发‘); } 2、react中使用this.setState,在onClick事件中不需要加小括号,但是在方法上要用箭头函数。下面这个例子也给出了回调函数的使用。 <button onClick={this.reState}>重新渲染</button> reState=()=>{ this.setState({ Name:‘Beautiful Girl!‘ },function(){console.log(‘问:this.setState在哪里使用箭头函数?回调函数中的state:‘+this.state.Name)}) console.log(‘重新reState的state值:‘+this.state.Name); } 3、react中传递参数,在onClick事件中需要使用箭头函数,在方法上不需要箭头函数。 <button onClick={()=>this.passMsg(‘问:要触发传参在哪里使用箭头函数?‘)}>传递参数</button> passMsg(arg1){ console.log(arg1); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |