我試圖擁有它,所以當一個人點擊“你的財富”按鈕時,會出現一個隨機的財富。相反,命運只是全部出現,而 onClick 無法按預期作業。我很困惑我可能在哪里搞砸了。任何幫助表示贊賞。
import React from "react";
import {Link} from "react-router-dom"
class Fortune extends React.Component {
constructor(props) {
super(props);
this.state = {
fortunes: [
"An exciting opporunity lies ahead",
"A long time friend will brirng wise advice in the coming week",
"You will find great fortunes in unexpected places",
"Never give up... Unless you want to thats cool too",
"111",
"222",
"333",
"444",
"Maybe a nap is what you need",
"Don't Text Your EX!"
],
fortune: ""
}
}
componentDidMount() {
this.getFortune();
}
getFortune = () => {
let rand = Math.floor(Math.random() * (this.state.fortunes.length) 0)
console.log(rand);
this.setState({
fortune: this.state.fortunes[rand]
})
}
render() {
return (
<body>
<div className="home-buttons-wrapper">
<button onClick={Fortune.getFortune}>Your Fortune</button>
<h5>{this.state.fortunes}</h5>
</div>
</body>
)
}
}
console.log('getFortune');
export default Fortune;
uj5u.com熱心網友回復:
onClick={Fortune.getFortune}
應該:
onClick={this.getFortune}
和
<h5>{this.state.fortunes}</h5>
應該沒有尾隨 's'(這就是您看到整個集合的原因)
<h5>{this.state.fortune}</h5>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/340778.html
標籤:javascript 反应
下一篇:從下面的代碼中,根據第2行條件-如果骰子===6代碼必須停止!在第4行重新分配骰子變數有什么需要?(我是一個begnr)
