frontend-opensource-project / use-react-hooks

React hoooooooks😎
https://frontend-opensource-project.github.io/use-react-hooks/
5 stars 0 forks source link

[URH-43] useDebounce μ‹ κ·œ #38

Closed suhyeoonn closed 3 months ago

suhyeoonn commented 3 months ago

πŸ‘Ύ Pull Request

1️⃣ Spec

2️⃣ λ³€κ²½ 사항

3️⃣ μ˜ˆμ‹œ μ½”λ“œ

function App() {
  const [searchText, setSearchText] = useState('');
  const [debouncedText, setDebouncedText] = useState(searchText);
  const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
    const newValue = e.target.value;
    setSearchText(newValue);
    debounceSearchText(newValue);
  };

  const debounceSearchText = useDebounce((text: string) => {
    setDebouncedText(text);
  }, 200);

  const filteredUsers = users.filter((u) =>
    u.name.toLowerCase().includes(debouncedText.toLowerCase())
  );

  return (
    <div>
      <input type="text" value={searchText} onChange={handleChange} />
      <ul>
        {filteredUsers.map((u) => (
          <li key={u.id}>{u.name}</li>
        ))}
      </ul>
    </div>
  );
}

4️⃣ κ΄€λ ¨ λ¬Έμ„œ (선택 사항)