Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
A React component is a reusable piece of UI that combines HTML structure and styling. Think of components as building blocks - like LEGO bricks - that you can combine to create complex interfaces.
Components are JavaScript functions that return JSX (HTML-like code). They encapsulate their own structure and can be reused throughout your application with different data.
function Welcome() {
return <h1>Hello!</h1>;
}<div> <h1>Hello!</h1> </div>
Props (short for "properties") are how you pass data from a parent component to a child component. Think of props as function parameters - they allow components to receive and display different data.
Props are read-only, meaning a component cannot modify the props it receives. This ensures predictable data flow from parent to child components.
Data flows in one direction: from parent to child
See props in action with a simple greeting component
Instead of writing props.name repeatedly, you can destructure props directly in the function parameters. This makes your code cleaner and more readable.
function UserCard(props) {
return (
<div>
<h2>{props.name}</h2>
<p>{props.role}</p>
<span>{props.avatar}</span>
</div>
);
}Repetitive props. prefix
function UserCard({ name, role, avatar }) {
return (
<div>
<h2>{name}</h2>
<p>{role}</p>
<span>{avatar}</span>
</div>
);
}Clean and readable!
Create rich components by passing multiple props
Dynamic styling and conditional rendering with inline to external CSS conversion
Sometimes you want a prop to have a default value if it's not provided. You can use default parameters in destructuring to set fallback values, making your components more flexible.
function Button({ text = "Click me", color = "blue" }) {
return (
<button style={{ backgroundColor: color }}>
{text}
</button>
);
}
// Usage examples:
<Button /> // Uses both defaults
<Button text="Submit" /> // text="Submit", color="blue"
<Button text="Delete" color="red" /> // Both customuserName not xdata1, data2