my previous question led me here and after being able to access and use musixmatch API successfully, I now see that not all the songs have lyrics
so I'm getting the songs but they are some songs that don't have their lyrics published yet and I want to handle this error
I first set the lyrics to setLyrics, then check if its undefined and use a spinner to load and then disappear when the fetch returns results
return trackInfo ===undefined||lyrics===undefined ||Object.keys(trackInfo).length===0
||Object.keys(lyrics).length===0 ?
<Spinner/>:<Songlyrics/>
but what if the resource never comes because it doesn't exist, the spinner just keeps loading indefinitely, I want the spinner to load for a few seconds but if there are no lyrics for a particular song, render a component that tells the user that the resource isn't available and reroute to the home page
I have tried using a setTimeout but now even if the lyric is available I'm still rerouted which tells me I'm missing something, I also need to figure out a way to render the noLyrics message component
return trackInfo ===undefined||lyrics===undefined ||Object.keys(trackInfo).length===0 ||Object.keys(lyrics).length===0 ?
<>
<Spinner/>
setTimeout(()=>{
navigate('/')
},6000)
</>
:<Songlyrics/>
can someone help me? for more context
function Lyrics() {
const [lyrics,setLyrics]=useState('')
const [loading,setLoading]=useState(false)
const navigate =useNavigate()
// get request for lyrics by id
useEffect(()=>{
setLoading(true)
fetch('getting lyrics')
.then(res=>res.json())
.then(data=>{
// setLyric to the lyric body
setLoading(false)
setLyrics(data.message.body.lyrics.lyrics_body)
})
},[])
return loading ? <Spinner/>:
(trackInfo ===undefined||lyrics===undefined ||Object.keys(trackInfo).length===0 ||Object.keys(lyrics).length===0?<DoesNotExist/>
:
(
<component/>
)
)
}