Skip to content

Instantly share code, notes, and snippets.

@mohitxskull
Last active January 10, 2023 04:53
Show Gist options
  • Save mohitxskull/c34da3ca989f429356c1f47f4b1e356c to your computer and use it in GitHub Desktop.
Save mohitxskull/c34da3ca989f429356c1f47f4b1e356c to your computer and use it in GitHub Desktop.
This code is for a React component that allows users to filter a list of items by searching for specific keywords
import { useEffect, useState } from "react";
// It uses the useState hook to manage the state of the filtered data,
// and an input field with an onKeyUp event listener to trigger the filtering function.
// When the function is called, it filters the list of items based on
// whether the item's name includes the search query, and updates the state of the filtered data.
// The component then renders the list of items or a message indicating that no data was found,
// depending on whether there are any items in the filtered data array.
const App = () => {
const myData = ["Apple", "Banana", "Orange", "Papaya", "Grapes"];
const [filteredData, setFilteredData] = useState(myData);
const [query, setQuery] = useState("");
useEffect(() => {
const filteredRecords = myData.filter((item) =>
item.toLowerCase().includes(query.toLowerCase())
);
setFilteredData(filteredRecords);
}, [query]);
return (
<div className="container">
<div className="search_box">
<input
type="text"
value={query}
onChange={(e) => setQuery(e.target.value)}
className="searchCtrl"
placeholder="Search the list"
/>
</div>
<div className="box">
{filteredData && filteredData.length > 0 ? (
<ul className="list">
{filteredData.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
) : (
<div className="message">Not Data Found</div>
)}
</div>
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment