Random quote machine, where is the error? --React

Hello everyone, I already built a random quote machine in jquery but now I am trying it in React.

const quoteArray = ['quote1', 'quote2', 'quote3']
class Display extends React.Component {
     this.state = {
       DisplayedQuote: 'yeet'
  getRandomQuote() {
    const diffQuotes = quoteArray.filter(state => state  !== this.state.DisplayedQuote);
    const randomQuote = Math.floor(Math.random() * diffQuotes.length);
    return diffQuotes[randomQuote];
  render() {
  return (
      <h1> {this.state.DisplayedQuote} </h1>  
      <button onClick={this.getRandomQuote()}> Change </button>

Where is the error(s) in my code? I am trying to get a new quote from the QuoteArray and have it displayed in DisplayedQuote.
I am quite new to react, so sorry if I am making simple mistakes.

Full code in case needed:

Getting a new random quote is not enough here, as you need to trigger re-render and in React you would do this by setState(newstate)

1 Like

So where should I put the setState exactly?
In the onclick statement?

Inside your getRandomQuote() - when you get it you need to update state

I haven’t found exactly where to put it yet, but I am making your answer as the solution just in case I forget.
Thanks for your help!

Try to put it instead of return :wink:

Sorry to bother you again, after getting lunch and coffee but I am still getting nothing.


Is this how setState would be used in place of a return?

No, setState is a function. So:

this.setState({ DisplayedQuote: diffQuotes[randomQuote] } );
1 Like

Thank you. Sorry for the excessive hand-holding I require. I am very bad with React.

      <h1> {this.state.DisplayedQuote} </h1>  
      <button onClick={this.getRandomQuote}> Change </button>

So what is wrong with my onClick statement? It is working on the previous project I did.