I'm using React to build a web application that will display a different quote every time the user clicks a button. I've been hunting for a way to get my component QuoteGenerator
to re-render/refresh on a button-click, and I've tried all the possibilities presented in this article with no success.
class QuoteGenerator extends React.Component {
constructor(props) {
super(props);
this.state = {num: Math.floor(Math.random() * quotes.length)};
}
render() {
return(
<div class="quoteBox">
<div class="quote">
"{quotes[this.state.num][0]}"<br/>
</div>
<button class="button btn" onClick={this.forceUpdate}>New Quote</button>
</div>
);
}
}
Even creating a separate function that runs this.forceUpdate()
or this.setState()
and calling that function with onClick
hasn't worked; I think I must be implementing something wrong. Does anyone see where my error might be?