Hi all, I am hoping to find a solution to this warning message that I received:
Line 20:4: React Hook useEffect has missing dependencies: ‘randomObject.author’ and ‘randomObject.text’. Either include them or remove the dependency array react-hooks/exhaustive-deps
Below is my code:
import React, {useState, useEffect} from "react"
import './App.css';
function App() {
const [data, setData] = useState([])
const [quote, setQuote] = useState("")
const [author, setAuthor] = useState("")
const randomIndex = Math.floor(Math.random() * data.length)
const randomObject = data[randomIndex]
useEffect(() => {
fetch("https://type.fit/api/quotes")
.then(response => response.json())
.then(data => {
setData(data)
setQuote(randomObject.text)
setAuthor(randomObject.author)
})
}, [])
const handleClick = () => {
setQuote(randomObject.text)
setAuthor(randomObject.author)
}
return (
<div className="container" id="quote-box">
<h1 id="text">{quote}</h1>
<h2 id="author">{author}</h2>
<button onClick={(handleClick)}>New Quote</button>
</div>
);
}
export default App;
If anyone has a solution for this, I would love to hear about it. Thanks!