-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.ts
38 lines (34 loc) · 958 Bytes
/
filter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import React, { useState, useTransition } from "react";
function Filter({ items }) {
const [filter, setFilter] = useState("");
const [startTransition, isPending] = useTransition();
const [inputValue, setInputValue] = useState("");
const filteredItems = items.filter((item) => item.name.includes(filter));
const handleFilter = () => {
startTransition(() => {
setFilter(inputValue);
});
};
return (
<div>
<input
type="text"
value={inputValue}
onChange={(e) => {
setInputValue(e.target.value);
startTransition(() => {
setFilter(e.target.value);
});
}}
placeholder="type to search"
/>
<button onClick={handleFilter}>Filter</button>
{isPending ? (
<div> Loading ... </div>
) : (
filteredItems.map((item) => <div key={item.name}> {item.name} </div>)
)}
</div>
);
}
export default Filter;