I am creating a random quote generator. Everything works fine. But the quote doesn't appear at the page reload. I have to click new quote button to get a new quote. It should also display a quote when the DOM is loaded.
My Code:
import { useCallback, useEffect, useState } from 'react';
import './App.css';
function App() {
const quotesURL ='https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json';
const [quotes, setQuotes] = useState([]);
const [randomQuote, setRandomQuote] = useState('');
const fetchQuotes = useCallback(() => {
fetch(quotesURL)
.then(response => response.json())
.then(data => setQuotes(data.quotes))
.catch(err => console.error(err));
// console.log(quotes);
}, [quotesURL]);
// eslint-disable-next-line react-hooks/exhaustive-deps
const handleNewQuote = () => {
const randomIndex = Math.floor(Math.random() * quotes.length);
setRandomQuote(quotes[randomIndex]);
};
useEffect(() => {
fetchQuotes();
}, []);
return (
<div>
<div id='quote-box'>
<div id='text'>{randomQuote.quote}</div>
<div id='author'>{randomQuote.author}</div>
<button id='new-quote' onClick={handleNewQuote}>
New Quote
</button>
<a href='https://www.twitter.com/intent/tweet' id='tweet-quote'>
Tweet Quote
</a>
</div>
{quotes?.map((quote, index) => (
<div key={index}>{JSON.stringify(quote)}</div>
))}
</div>
);
}
export default App;
What I want is to display a quote at the reload of the page. It currently works only when the button is clicked!
The problem according to my understanding is that the useEffect calls fetchQuotes() after handleNewQuote(), so there is no quote to load
handleNewQuotewill only be called when clicking your "New Quote" button.fetchQuoteswill be called right after the component has been mounted, and thus not making possible to be called if you have not clicked the button.