4.2 Components and Props โ€“ The Building Blocks of React

Table of Contents

Hey little coder! ๐Ÿ‘‹
In our last React session, we talked about what React is and why itโ€™s amazing.
Now letโ€™s dive deeper into the real heroes of React: components and props. ๐Ÿฆธโ€โ™‚๏ธ

These are like the bricks and glue that hold everything together in a React app.


๐Ÿงฉ What Are Components?

Think of components as tiny machines ๐Ÿ›  that you build once and use everywhere.

Like how you use a LEGO block to make different houses. ๐Ÿงฑ

In React, we have two main types of components:

TypeWhat it doesHow it looks
FunctionalJust a function that returns UISimple and clean
Class-basedOlder way with more features built-inA bit longer

๐Ÿง  Functional Components (The Modern Way)

These are simple functions that return JSX.

โœ… Example:

jsxCopyEditfunction Welcome() {
  return <h2>Hi, Iโ€™m a functional component! ๐Ÿ‘‹</h2>;
}

Then render it like this:

jsxCopyEditReactDOM.render(<Welcome />, document.getElementById("root"));

Functional components are lightweight, easy to read, and perfect for beginners.


๐Ÿง™โ€โ™‚๏ธ Class Components (Old School)

Still useful for legacy apps, but less common today.

โš ๏ธ Slightly complex example:

jsxCopyEditclass Welcome extends React.Component {
  render() {
    return <h2>Hello from a class component! ๐Ÿง™โ€โ™‚๏ธ</h2>;
  }
}

Use them when you need more control, like managing complex state (though we now use hooks instead in functional components).


๐ŸŽ What are Props?

Props (short for properties) are how you pass data between components.

Letโ€™s say you have a Greeting component and want it to show different names.

๐Ÿค” Example:

jsxCopyEditfunction Greeting(props) {
  return <h3>Hello, {props.name}! ๐Ÿ˜Š</h3>;
}

Now use it like this:

jsxCopyEdit<Greeting name="Vikas" />
<Greeting name="Ananya" />
<Greeting name="Web Codder Fan" />

โœ… This is called component reusability.
โœ… You just used the same component 3 times with different data!


๐Ÿ“ฆ Visual: How Props Work

22907 01 6 Item Connection Infographic Powerpoint Template 16X9 1
4.2 Components And Props โ€“ The Building Blocks Of React 3

A parent passes data โžก๏ธ child component receives it as props


๐Ÿง’ Children in React

Sometimes, you want to pass whole elements inside a component.
Thatโ€™s where children come in.

Example:

jsxCopyEditfunction Wrapper(props) {
  return <div className="box">{props.children}</div>;
}

<Wrapper>
  <p>This is inside the box! ๐Ÿ“ฆ</p>
</Wrapper>

Here, props.children means โ€œwhatever you put between the opening and closing tags.โ€


๐ŸŒฑ Default Props

Sometimes, you want to show default content if nothing is passed.

Example:

jsxCopyEditfunction Hello(props) {
  return <p>Hello, {props.name}!</p>;
}

Hello.defaultProps = {
  name: "Stranger",
};

So if you use <Hello /> without passing a name, it shows:
๐Ÿ‘‰ Hello, Stranger!


๐Ÿ›  Breaking UI into Reusable Components

Big apps are hard to manage if everything is in one file. ๐Ÿ˜“
So letโ€™s break them into smaller pieces.

Example: Building a profile card

jsxCopyEditfunction Avatar(props) {
  return <img src={props.url} alt="Profile" />;
}

function Name(props) {
  return <h4>{props.name}</h4>;
}

function ProfileCard() {
  return (
    <div className="card">
      <Avatar url="https://via.placeholder.com/150" />
      <Name name="Vikas Sankhla" />
    </div>
  );
}

๐ŸŽ‰ Now each part (Avatar and Name) is its own component.
Easy to maintain and reuse in other parts of your app.


โš–๏ธ Props vs State (Quick Preview)

Letโ€™s keep this simple:

FeaturePropsState
PurposePass data from parentStore data inside component
Mutable?โŒ No (read-only)โœ… Yes (can change)
Who sets it?Parent componentComponent itself

We’ll learn state more in the next part ๐Ÿ˜‰


๐Ÿง  Key Concepts Recap

ConceptWhat It Means
ComponentA piece of the UI you can reuse
Functional ComponentA simple function that returns JSX
PropsData passed from parent to child
ChildrenElements passed inside a component
Default PropsFallback values if no prop is passed
ReusabilityUsing the same component in many places

๐Ÿง  Mind Map: Components + Props

Mind Map Placeholder
4.2 Components And Props โ€“ The Building Blocks Of React 4

Visually showing: App โ†’ Parent โ†’ Child โ†’ Props โ†’ JSX โ†’ Reuse

๐Ÿ™Œ Letโ€™s Keep Learning Together!

If you’re loving this React series, donโ€™t forget to support Web Codder! ๐ŸŽ‰

Your support keeps this free content coming! ๐Ÿ™


๐Ÿ‘‹ Final Words

React becomes easier once you understand components and props.
Theyโ€™re the LEGO blocks ๐Ÿงฑ of your app โ€” build small, combine big.

Iโ€™m Vikas Sankhla from Web Codder โ€” your tech buddy!
Until next time, keep building, keep breaking (code only ๐Ÿ˜„), and stay curious.

Share the Post:
Picture of Web Codder

Web Codder

Vikas Sankhla is a seasoned Full Stack Developer with over 7 years of experience in web development. He is the founder of Web Codder, a platform dedicated to providing comprehensive web development tutorials and resources. Vikas specializes in the MERN stack (MongoDB, Express.js, React.js, Node.js) and has been instrumental in mentoring aspiring developers through his online courses and content. His commitment to simplifying complex web technologies has made him a respected figure in the developer community.

Related Posts