I need help with a React.js problem.
I am making a request with user input on a search bar I am using the hook useDebounce and when the user clears the input field after they got a result back. The array data becomes empty and the map function can not run on an empty array.
my question is
How do I check for empty array when the user clears out the input field.
App.jsx
// debounced query
let {debouncedQuery} = useContext(DataContext)
useEffect(() => {
// get the images of the breed that is choosen
const loadByBreed = async (value: string) => {
if (!debouncedQuery) return
try {
const response = await fetch(`https://dog.ceo/api/breed/${value}/images`)
const data = await response.json()
console.log(data)
setImagesOfABreed(data.message)
} catch (error) {
const err = error as Error
console.log(err.message)
}
}
loadByBreed(debouncedQuery);
}, [debouncedQuery])
{ debouncedQuery && imagesOfABreed?.length ? imagesOfABreed.map((data) => {
return (
<Card key={data} img={data} />
)
}) : <p>Nothing</p>}