Learn how to use props to pass data between React components. Master unidirectional data flow to build dynamic, reusable UIs for your movie browser.
Previously in this course, we learned about creating static components to build our app's skeleton. While static components are a good start, a real application needs to be dynamic.
Today, we’re moving beyond hard-coded UI by learning how to pass information between components. In React, this is achieved through props.
"Props" is short for properties. Think of them as the arguments you pass to a JavaScript function, but for your React components. They allow a parent component to send data down to a child component, making your UI modular and reusable.
In React, data flow is unidirectional. This means data always travels from the top (parent) down to the bottom (child). By mastering this flow, you ensure your application remains predictable and easy to debug, as we explore in React props vs state: Mastering Unidirectional Data Flow.
To pass data, you add attributes to your component in JSX, just like you would with standard HTML attributes.
Imagine we are building our movie browser project. Instead of hard-coding a single movie title, we want a MovieTitle component that can display any title we give it.
Here is how you pass data to a child:
JSX// ParentComponent.jsx import MovieTitle from CE9178">'./MovieTitle'; function App() { return ( <div> {/* We pass the CE9178">'title' attribute here */} <MovieTitle title="The Matrix" /> <MovieTitle title="Inception" /> </div> ); }
When you pass an attribute, React bundles all those attributes into a single JavaScript object called props. The child component receives this object as its first argument.
JSX// MovieTitle.jsx function MovieTitle(props) { // props is { title: "The Matrix" } return <h1>{props.title}</h1>; }
Because props is just a standard object, we often use object destructuring to make our code cleaner:
JSXfunction MovieTitle({ title }) { return <h1>{title}</h1>; }
Let’s advance our movie browser project. We want a MovieCard that accepts both a title and a release year.
JSX// MovieCard.jsx function MovieCard({ title, year }) { return ( <div className="movie-card"> <h2>{title}</h2> <p>Released: {year}</p> </div> ); } // App.jsx function App() { return ( <div> <MovieCard title="The Matrix" year="1999" /> <MovieCard title="Inception" year="2010" /> </div> ); }
By changing the attributes in App.jsx, the MovieCard component updates automatically. This is the core of component communication in React.
Greeting.jsx.name prop and renders an <h1> saying "Hello, [name]!".Greeting into your App.jsx and render it twice with different names (e.g., <Greeting name="Alice" />).props.title = "New Title" will not work and is considered an anti-pattern). If data needs to change over time, use state, which we will cover in later lessons.<MovieCard year={1999} />. If you write year="1999", React treats it as the string "1999".movieTitle, you must access it as props.movieTitle, not props.movietitle.We've covered the fundamentals of passing data via props. Key takeaways:
props object in your function signature.Understanding these patterns is essential for managing React state management and the unidirectional data flow as your application grows in complexity.
Up next: We'll take what we've learned and build more complex, Dynamic Movie Cards that leverage these concepts to display full movie details.
Master the art of UI state management by adding a filter toggle to your movie app. Learn to control list rendering using boolean state and checkbox inputs.
Passing Data with Props
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