I have an API that sometimes doesn’t work. I would like for the App to refetch automaticaly if this happens until it gets the necessary data. How can I do that? I’m thinking that maybe this could be done by using a dependency on the useEffect
hook, but I’m not clear on how to do it.
Lets say we have this App component
export default function App() {
const [data, setData] = useState([])
useEffect(() => {
getData({ setData })
}, [])
return [
<h3>
{data[0].title}
</h3>
]
}
And this API component
const url = 'https://some-random-url.com/whatever-api'
export default function getData({ setData }) {
axios.get(url)
.then((response) => {
let dataArray = response.data.results
setData(dataArray)
})
.catch((error) => {
console.log(error)
})
}