Introduction
Parent-child data flow helps components share information clearly. The parent sends data down to the child, so the child can use or show it.
Parent-child data flow helps components share information clearly. The parent sends data down to the child, so the child can use or show it.
function Parent() { const data = 'Hello from parent'; return <Child message={data} />; } function Child(props) { return <p>{props.message}</p>; }
The parent passes data as props to the child.
The child receives data via the props object.
userName prop to child, which shows a welcome message.function Parent() { const name = 'Alice'; return <Child userName={name} />; } function Child(props) { return <h1>Welcome, {props.userName}!</h1>; }
number directly.function Parent() { const count = 5; return <Child number={count} />; } function Child({ number }) { return <p>Count is {number}</p>; }
This example shows a parent component sending a greeting message to its child. The child displays the message inside a paragraph.
import React from 'react'; function Parent() { const greeting = 'Hi there!'; return ( <div> <h2>Parent Component</h2> <Child message={greeting} /> </div> ); } function Child(props) { return ( <div> <h3>Child Component</h3> <p>Message from parent: {props.message}</p> </div> ); } export default Parent;
Data flows only one way: from parent to child.
Children cannot change props; they are read-only.
Use props to keep components simple and reusable.
Parent sends data to child using props.
Child receives data via props and uses it to render.
This keeps data flow clear and easy to follow.
title with value "Hello" from a parent to a child component Child?<Child /> component?function Parent() {
const message = "Hi there!";
return <Child text={message} />;
}
function Child(props) {
return <h1>{props.text}</h1>;
}function Parent() {
const data = "Hello";
return <Child data=data />;
}
function Child(props) {
return <div>{props.data}</div>;
}data=data without braces, so it passes string "data" instead of variable value.function Parent() {
const items = ["Apple", "Banana", "Cherry"];
return <Child items={items} />;
}
function Child(props) {
return (
<ul>
{props.items.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);
}Pass the test and this pattern moves unknown