[英]Using the Math.pow() method inside of setState()
在我的 reactjs 代碼示例中,方法 Math.pow() 必須在每次單擊按鈕時更改 div 元素的值,但該方法不起作用,請解釋一下,為什么。
handlerButton=(e)=> {
this.setState(function(prevState) {
return({number: Math.pow(prevState.number, 2)});
});
}
改變:
<MyButton handlerButton={this.handlerButton}/>
到 :
<MyButton onClick={this.handlerButton}/>
<MyButton handlerButton={this.handlerButton}/>
您傳入了一個名為handlerButton
的道具,但您從未在 MyButton 中使用過 props.handlerButton。 相反,您使用的是 props.onClick
render() {
return <button id="btn" className="mybutton" onClick={this.props.onClick}>pow</button>;
}
您需要在兩個地方使用相同的道具名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.