Nothing Found چالش 4 از جلسهی 24، متوسط
جستوجوی حیوانها کار میکند. دو چیز به آن اضافه کن که هر دو از روی shown حساب میشوند:
- یک p با id برابر info که بنویسد چند مورد از چند مورد دیده میشود: 4 of 4
- وقتی هیچ موردی پیدا نشد، یک p با id برابر empty و متن Nothing found. در بقیهی وقتها روی صفحه نباشد.
<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
ul { list-style: none; padding: 0; }
li { margin: 6px 0; }
</style>
<div id="root" dir="ltr"></div>
<script type="text/jsx">
import { useState } from "react";
import { createRoot } from "react-dom/client";
function App() {
const animals = ["cat", "dog", "cow", "fox"];
const [query, setQuery] = useState("");
const shown = animals.filter((animal) => animal.includes(query.toLowerCase()));
return (
<div>
<input id="search" placeholder="Search" value={query} onChange={(e) => setQuery(e.target.value)} />
<ul id="list">
{shown.map((animal) => (
<li key={animal}>{animal}</li>
))}
</ul>
</div>
);
}
createRoot(document.getElementById("root")).render(<App />);
</script>
راهنما و جواب این چالش · برگشت به درس جلسهی 24: فیلتر و جستوجو · چالش بعدی: Fruit or Veg
بیشتر بخوان: جاوااسکریپت چیست؟ از دکمهی وب تا Node.js