Hackforge Academy

Category: react

user filter in react

Published on 17 Apr 2026

Explanation

Import React hooks. We use useState to store users and filter text, and useEffect to fetch users from the API when the component loads βš›οΈ.

Code:

import React, { useEffect, useState } 
from "react";

Explanation

Create state variables. One state stores the users list and another stores the search input value for filtering users

Code:

function UsersFilter() {
  const [users, setUsers] = useState([]);
  const [search, setSearch] = useState("");
}

Explanation

Fetch users from API using useEffect and store them in state. This runs once when the component loads.

Code:

useEffect(() => {
  fetch("https://www.apirequest.in")
    .then((res) => res.json())
    .then((data) => setUsers(data));
}, []);

Explanation

Slide 4: Filter users dynamically based on search input using JavaScript filter(). This enables real-time search functionality πŸ“‹.

Code:

const filteredUsers = users.filter((user) =>
  user.name.toLowerCase().includes(
search.toLowerCase())
);

Explanation

Add input box and display filtered users using map(). The list updates automatically as the user types .

Code:

return (
  <div>
    <input
      type="text"
      placeholder="Search user..."
      onChange={(e) => 
setSearch(e.target.value)}
    />

    <ul>
      {filteredUsers.map((user) => (
        <li key={user.id}>
          {user.name}
        </li>
      ))}
    </ul>
  </div>
);
}

export default UsersFilter;

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community