Master the useState hook to add interactivity to your React components. Learn how to initialize and update local state to build dynamic user interfaces.
Previously in this course, we discussed the core concept of introduction to react state: making your ui interactive, which explained why standard JavaScript variables aren't enough to trigger UI updates in React. In this lesson, we’ll move from theory to implementation by mastering the useState hook.
By the end of this lesson, you will be able to import useState, initialize state variables, and trigger re-renders by updating state with setter functions.
In React, a component is a function that returns UI. When data changes, you want that UI to update automatically. However, local variables inside a function are reset every time that function runs. React solves this with hooks, specifically useState.
Think of useState as a "memory slot" that React manages for your component. When you call useState, you provide an initial value. React returns an array with exactly two elements:
Let's add a "Like" button to our MovieCard component. We want to track whether a user has liked a movie.
First, import the hook from React at the top of your file:
JAVASCRIPTimport { useState } from CE9178">'react';
Inside your component, call the hook. We use array destructuring to grab the two values returned by useState:
JAVASCRIPTfunction MovieCard({ title }) { // Initialize state: CE9178">'isLiked' is the value, CE9178">'setIsLiked' is the updater const [isLiked, setIsLiked] = useState(false); return ( <div className="movie-card"> <h3>{title}</h3> <button onClick={() => setIsLiked(!isLiked)}> {isLiked ? CE9178">'❤️ Liked' : CE9178">'🤍 Like'} </button> </div> ); }
useState(false) tells React that the initial value of isLiked should be false.setIsLiked is the only way you should ever change this value. Calling it tells React: "The data changed, please re-run this component function and update the DOM."MovieCard function again. This time, isLiked holds the new value, and the button text updates accordingly.In your movie-browser project, locate your MovieCard component. I want you to:
views starting at 0.onClick handler to increment the views count every time the button is clicked.Hint: You can use setIsViews(views + 1) inside your event handler.
Even experienced developers trip over these three common issues with useState:
isLiked = true). React won't know the value changed, and your UI will remain stale. Always use the setter function.useState at the top level of your component. Never place it inside an if statement or a loop. React relies on the order of hooks to keep track of which state belongs to which component.You’ve now moved beyond static markup. By using useState, you allow your components to "remember" data and respond to user interactions. Remember: useState gives you the current value and a setter function. Use the setter to trigger updates, and always keep your hooks at the top level of your functional components.
Up next: We'll dive into react form handling: controlled vs. uncontrolled components to synchronize input fields with state for a professional search experience.
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 moreLearn to create custom hooks in React to abstract complex data-fetching logic. Improve your code reusability and simplify your components by building a useFetch.
Managing State with useState
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