NextJS, button, onClick, show, hide, no data found #176198
-
BodyHow to show a "No data found" when map length = 0 alert or notification? The button has a handle submit function that fires when the button is clicked. Submit function Button Results array Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @Fred638, this suggestion to you.. So if I understand correctly, you want the "No data found" message to only appear after the button is clicked and the results come back empty, not before? If that's the case, you probably need to track whether a search has been performed. Try adding a state like this: const [hasSearched, setHasSearched] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setHasSearched(true);
// ... your existing fetch logic
}
return (
<div>
{hasSearched && results.length === 0 ? (
<div>No data found!</div>
) : (
results.map(...)
)}
</div>
) This way the "No data found" message only shows up after someone actually clicks the button and gets zero results, not on initial page load. |
Beta Was this translation helpful? Give feedback.
Hi @Fred638, this suggestion to you.. So if I understand correctly, you want the "No data found" message to only appear after the button is clicked and the results come back empty, not before?
If that's the case, you probably need to track whether a search has been performed. Try adding a state like this:
This way the "No data found" message only shows up a…