Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 15 of the React Fundamentals: Build Modern UIs from Scratch course
ReactJune 25, 20263 min read

Building an Interactive Search Bar: Controlled Inputs in React

Learn to build an interactive search bar in React. Master controlled inputs by synchronizing form values with local state for a reactive, responsive UI.

reactjavascriptfrontendhooksformsweb development

Previously in this course, we explored introduction to react state: making your ui interactive and mastered managing state with usestate: a beginner's guide to react hooks. In this lesson, we apply those fundamentals to build a functional search bar, moving from static HTML to dynamic, data-driven inputs.

The Concept: Controlled Inputs

In standard HTML, an <input> field maintains its own internal state. When a user types, the browser automatically updates the display. However, in React, we prefer the "Single Source of Truth" pattern.

We achieve this using controlled inputs. A controlled input is a form element whose value is driven by React state rather than the browser's internal DOM state. When the user types, we capture the event, update our state, and React re-renders the component to display the new value.

This approach gives us full control over the data, allowing us to validate, transform, or log input in real-time.

Building the Search Bar

To build our search bar, we need two things: an input element and a piece of state to track what the user is typing. We’ll use the onChange event listener to trigger a state update every time a key is pressed.

Here is how you implement it in your SearchBar component:

JSX
import { useState } from CE9178">'react';

function SearchBar() {
  const [searchTerm, setSearchTerm] = useState(CE9178">'');

  const handleChange = (event) => {
    const newValue = event.target.value;
    setSearchTerm(newValue);
    
    // Logging state changes to the console
    console.log("Current search term:", newValue);
  };

  return (
    <div className="search-container">
      <input
        type="text"
        placeholder="Search for movies..."
        value={searchTerm}
        onChange={handleChange}
      />
      <p>Searching for: {searchTerm}</p>
    </div>
  );
}

export default SearchBar;

Breaking down the code:

  1. useState(''): We initialize our state with an empty string. This represents the starting value of the search bar.
  2. value={searchTerm}: This is the "controlled" part. We tell the input that its value must always match the searchTerm state variable.
  3. onChange={handleChange}: Whenever the user types, the browser fires an event. We access event.target.value to get the current characters in the box and update our state.

Hands-on Exercise

It's time to advance our movie-browser project. Follow these steps:

  1. Create a new file named SearchBar.jsx in your components folder.
  2. Implement the code above, ensuring you import useState from React.
  3. Add this component to your main App.jsx file to see it in action.
  4. Open your browser's Developer Tools (F12) and go to the "Console" tab. Type into the input field and confirm that every keystroke logs the updated state correctly.

Common Pitfalls

Even experienced developers run into these issues when starting with forms:

  • Forgetting the onChange handler: If you provide a value prop to an input but don't provide an onChange handler, the input will become read-only. You won't be able to type anything!
  • Assuming State is Synchronous: Remember that calling setSearchTerm does not update the variable immediately in the current execution context. If you console.log(searchTerm) immediately after setting it, you will see the old value. Always use the event object (event.target.value) if you need the immediate new value.
  • Over-complicating state: For a simple search bar, you don't need complex patterns. Keep it simple with useState until you reach the requirements of react form handling: controlled vs. uncontrolled components.

Recap

By controlling our input, we've bridged the gap between user interaction and application state. We've learned that:

  • Controlled inputs allow React to own the data within a form field.
  • The value prop links the input to our useState variable.
  • The onChange event is the primary mechanism for capturing user input.

You now have a responsive search bar that tracks user intent. This component is the foundation for the filtering logic we will build in upcoming lessons.

Up next: We will explore Handling Click Events to make our application even more interactive.

Previous lessonManaging State with useStateNext lesson Handling Click Events
Back to Blog

Similar Posts

ReactJune 25, 20263 min read

Extracting Custom Hooks: A Guide to React Reusability

Learn to create custom hooks in React to abstract complex data-fetching logic. Improve your code reusability and simplify your components by building a useFetch.

Read more
ReactReactJune 25, 20263 min read

Fetching Data from an API: A Practical React Guide

Part of the course

React Fundamentals: Build Modern UIs from Scratch

beginner · Lesson 15 of 49

  1. 1

    Introduction to Component-Based Architecture

    4 min
  2. 2

    Understanding the Virtual DOM

    4 min
  3. 3

    Setting Up with Vite

    3 min

Master the fetch API in React to retrieve external data. Learn to perform asynchronous requests and store JSON responses in your component state effectively.

Read more
ReactJune 25, 20264 min read

Introduction to Side Effects: Managing External Logic in React

Learn why side effects like API calls belong in the useEffect hook. Distinguish between rendering and side effects to build predictable React applications.

Read more
4

Mastering JSX Syntax

4 min
  • 5

    Creating Static Components

    3 min
  • 6

    Styling Components

    3 min
  • 7

    Passing Data with Props

    3 min
  • 8

    Dynamic Movie Cards

    3 min
  • 9

    Component Composition

    3 min
  • 10

    Conditional Rendering

    3 min
  • 11

    Rendering Lists of Data

    4 min
  • 12

    The Key Prop Explained

    4 min
  • 13

    Introduction to React State

    4 min
  • 14

    Managing State with useState

    3 min
  • 15

    Building an Interactive Search Bar

    3 min
  • 16

    Handling Click Events

    4 min
  • 17

    Updating State Based on Previous State

    4 min
  • 18

    Filtering the Movie List

    3 min
  • 19

    Handling Form Submissions

    3 min
  • 20

    Controlled Components

    4 min
  • 21

    Event Bubbling and Propagation

    3 min
  • 22

    Building a Movie Filter Toggle

    3 min
  • 23

    Introduction to Side Effects

    4 min
  • 24

    useEffect Dependencies

    4 min
  • 25

    Fetching Data from an API

    3 min
  • 26

    Handling Loading States

    3 min
  • 27

    Managing Errors

    3 min
  • 28

    Cleanup Functions in useEffect

    3 min
  • 29

    Debouncing Search Input

    3 min
  • 30

    Refactoring for Clean Code

    3 min
  • 31

    Folder Structure Best Practices

    4 min
  • 32

    Extracting Custom Hooks

    3 min
  • 33

    Prop Drilling and Context API

    3 min
  • 34

    Polishing the UI

    Coming soon
  • 35

    Finalizing the Movie Browser

    Coming soon
  • 36

    Review of Component Lifecycle

    Coming soon
  • 37

    Review of State Management

    Coming soon
  • 38

    Building a Modal Component

    Coming soon
  • 39

    Introduction to PropTypes

    Coming soon
  • 40

    Performance Optimization Basics

    Coming soon
  • 41

    Handling Browser History

    Coming soon
  • 42

    Working with LocalStorage

    Coming soon
  • 43

    Building a Favorites List

    Coming soon
  • 44

    Handling Media in React

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Debugging React Apps

    Coming soon
  • 47

    Deployment Basics

    Coming soon
  • 48

    Using External Libraries

    Coming soon
  • 49

    Advanced

    Coming soon
  • View full course