Learn to build an interactive search bar in React. Master controlled inputs by synchronizing form values with local state for a reactive, responsive UI.
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.
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.
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:
JSXimport { 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;
useState(''): We initialize our state with an empty string. This represents the starting value of the search bar.value={searchTerm}: This is the "controlled" part. We tell the input that its value must always match the searchTerm state variable.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.It's time to advance our movie-browser project. Follow these steps:
SearchBar.jsx in your components folder.useState from React.App.jsx file to see it in action.Even experienced developers run into these issues when starting with forms:
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!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.useState until you reach the requirements of react form handling: controlled vs. uncontrolled components.By controlling our input, we've bridged the gap between user interaction and application state. We've learned that:
value prop links the input to our useState variable.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.
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 moreMaster the fetch API in React to retrieve external data. Learn to perform asynchronous requests and store JSON responses in your component state effectively.
Building an Interactive Search Bar
Polishing the UI
Finalizing the Movie Browser
Review of Component Lifecycle
Review of State Management
Building a Modal Component
Introduction to PropTypes
Performance Optimization Basics
Handling Browser History
Working with LocalStorage
Building a Favorites List
Handling Media in React
Introduction to Testing
Debugging React Apps
Deployment Basics
Using External Libraries
Advanced