How can i not repeat my code?

i started the random quote project, but i repeat my code in the firstClick() and firstQuote() function.
can you help me please ?
A Pen by Dylan (codepen.io)

I only see an afterClick function.

FYI - The following does not need the functional version of setState since you are not trying to base the new value of quote on the old version of it.

this.setState(state=>({
   quote:state.quote=randomQuote(quotes)
}))

The following will suffice:

this.setState({
  quote: randomQuote(quotes)
})

Update: Nevermind, I see what you are talking about now. Just make randomQuote and quotes global and in in initialize the state property quote to be the return value of calling randomQuote.to Then, in afterClick, use this.setState to assign the return value of calling randomeQuote again to the state property quote.

1 Like

The other option is making randomeQuote a method of the class Quote instead of a global function. Then, you would just add this. before calling randomQuote.

1 Like

okkay, i see.
and please, how can i ‘synchoron’ the value of the authors array in the component Author with the quotes of the component Quote ? is it possible to store multiple component in a global store ?

Instead of having randomQuote, you could just have randomIndex and have the function return an index that could be used by both arrays, regardless of where they are stored.

1 Like

okay i’ll try it,thanks you