【发布时间】:2020-11-28 21:57:33
【问题描述】:
我有一个数据数组。
const quotes = [
{"author": "Marilyn Monroe",
"quote" : "I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best." },
{"author" : "Oscar Wilde",
"quote" : "Be yourself, everyone else is already taken."}
]
我想将单个对象渲染到 div。然后我想在单击按钮时循环遍历数组中的对象
这是我迄今为止尝试过的:
class ‘MyComponent extends React.Component {
constructor(props) {
super(props);
this.sayHello = this.sayHello.bind(this);
}
sayHello() {
return quotes[(Math.floor(Math.random() * Math.floor(quotes.length)))];
}
render() {
return (
<div className="body">
<div className="quote">
<p>“ {this.sayHello.quote} “</p>
</div>
<button type="button" onClick={this.sayHello.quotes.quote}>New Quote</button>
</div>
);
}
};
函数创建(道具){ 返回 ( {this.sayHello.author} ) };
当我单击按钮时,我希望能够获得一个呈现给 div 的新对象。 我对反应非常陌生。提前感谢您的所有帮助。
【问题讨论】:
标签: javascript reactjs