๐ฅ๏ธ Passing data into components
In the previous step, we displayed a list of game titles on the page.
In this step, we will pass the game data into a component so that we can display the information in a more user-friendly way.
Understanding Props in React Components
Section titled โUnderstanding Props in React ComponentsโReact components can accept inputs called โpropsโ (short for properties). Props allow us to pass data from a parent component to a child component, making our components reusable with different data.
Step 1: Define Props for the GameCard Component
Section titled โStep 1: Define Props for the GameCard ComponentโFirst, we need to define what props our GameCard
component will accept. Weโll create an interface to specify the expected props and their types.
interface GameCardProps { title: string; releaseDate: string;}export default function GameCard({ title, releaseDate }: GameCardProps) { const formattedDate = releaseDate.substring(0, 10);
Step 2: Update the GameCard Component to Use Props
Section titled โStep 2: Update the GameCard Component to Use PropsโNow that weโve defined our props, we need to update the component to use them instead of hardcoded values.
return ( <div className="flex flex-col gap-4"> <div className="relative h-72 overflow-hidden"> <img src="https://picsum.photos/400/300" alt="Game Cover" className="w-full object-cover rounded-md" /> </div> <div className="flex justify-between"> <div className="flex flex-col justify-between w-2/3"> <h3 className="font-bold text-2xl text-slate-300">Game Title</h3> <h3 className="font-bold text-2xl text-slate-300">{title}</h3> <p className="text-cyan-300 uppercase text-sm font-semibold">Genre</p> <p className="text-slate-200/60 text-sm font-semibold">Start date</p> <p className="text-slate-200/60 text-sm font-semibold"> {formattedDate} </p> </div>
Step 3: Update the Index Route to Pass Props to GameCard
Section titled โStep 3: Update the Index Route to Pass Props to GameCardโFinally, we need to update our index route to pass the game data to the GameCard
component.
return ( <div className="flex items-center justify-center min-h-screen"> <div> <h1 className="text-4xl font-bold">Hello, World!</h1> <GameCard /> {games.map((game) => ( <div key={game.id}> <h2>{game.title}</h2> </div> ))} </div> <div className="container mx-auto px-8 grid grid-cols-3 gap-8"> {games.map((game) => ( <div key={game.id}> <GameCard key={game.id} title={game.title} releaseDate={game.releaseDate} /> </div> ))} </div>);
What Weโve Learned
Section titled โWhat Weโve LearnedโIn this tutorial, weโve:
- Created a TypeScript interface to define the props for our
GameCard
component - Updated the
GameCard
component to use these props instead of hardcoded values - Modified our index route to pass game data to the
GameCard
component - Improved the layout of our page using CSS Grid
Now our application displays each game from the database in a visually appealing card format, making the information more user-friendly and organized.
Congratulations! Youโve successfully learned how to pass data from a parent component to a child component using props. This is a fundamental concept in React that allows you to create reusable components that can display different data based on the props they receive.