react 自我小计

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);
    }

猜你喜欢

转载自www.cnblogs.com/qingshanyici/p/10176814.html