From the course: Master React 19 and Next.js 16 with Hands-On Projects and Real-World Applications

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Controlled props pattern

Controlled props pattern

This session will guide you through implementing the Controlled Props pattern in React. This pattern allows you to manage a component's state either internally, within the component itself, uncontrolled, or externally by a parent component, controlled. By the end of this session, you'll understand how to build flexible and reusable components that can adapt to different state management needs. Let's start with a simple React app. Let's create a Components folder inside src and add Rating.jsx, a reusable component for displaying and selecting ratings. Moving to Rating.jsx, let's declare a functional component called Rating using an arrow function. If value is provided, this makes the component controlled. Parent manages the state. I will explain this in detail later. If no default value is passed, it defaults to 0. This is for uncontrolled mode. The default number of stars is 5. OnChange is a callback function fired when the rating changes, e.g. user clicks a star. Let's add the…

Contents