简体   繁体   English

在 setState() 中使用 Math.pow() 方法

[英]Using the Math.pow() method inside of setState()

In my example of reactjs code the method Math.pow() must change the value of div element on every click of button, but the method doesn't work, explain me, please, why.在我的 reactjs 代码示例中,方法 Math.pow() 必须在每次单击按钮时更改 div 元素的值,但该方法不起作用,请解释一下,为什么。

handlerButton=(e)=> {           
   this.setState(function(prevState) {
   return({number: Math.pow(prevState.number, 2)});     
    }); 
}   

Change:改变:

<MyButton handlerButton={this.handlerButton}/>

to :到 :

<MyButton onClick={this.handlerButton}/>
<MyButton handlerButton={this.handlerButton}/>

You're passing in a prop named handlerButton , but you never use props.handlerButton inside MyButton.您传入了一个名为handlerButton的道具,但您从未在 MyButton 中使用过 props.handlerButton。 Instead, you're using props.onClick相反,您使用的是 props.onClick

render() { 
    return <button id="btn" className="mybutton" onClick={this.props.onClick}>pow</button>;  
}

You need to use the same prop name in both places.您需要在两个地方使用相同的道具名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM