• Introduction In this chapter, we’ll learn about React components! Components are the building blocks of a React application. Geek and Gal are here to help you understand components in an easy and fun way!
Gal Normal

Hey there! Ready to learn about React components? 🚀

Geek Curious

Yes! What's a React component, exactly?

Gal Happy

A React component is like a reusable piece of your app's user interface! You can combine components to build more complex UIs.

  • React Components
Gal Pleased

Components are written as JavaScript functions or classes. They take in "props" as input and return a piece of the user interface as output.

Geek Curious

What are "props"?

Gal Normal

Props are short for "properties." They're like inputs you can pass to a component to customize its appearance or behavior!

Gal Pleased

Here's a simple example of a functional component in React:

function Welcome(props) {
  return <h1>Hello, {props.name}!</h1>;
}
Geek Happy

I see! So a component is a function that takes props and returns UI elements!

Gal Happy

That's right! To use a component, you write it as a custom HTML tag. Like this:

<Welcome name="Sara" />
Geek Surprised

Oh, it looks like an HTML tag! But it's actually a React component, right?

Gal Happy

Exactly! And you can reuse components by just changing the props!

  • Component Composition
Gal Pleased

One of the coolest things about React components is that you can compose them. That means you can use one component inside another!

Geek Curious

Really? How does that work?

Gal Happy

You just include the component you want to use as a child of another component. Like this:

function App() {
  return (
    <div>
      <Welcome name="Sara" />
      <Welcome name="Cahal" />
      <Welcome name="Edite" />
    </div>
  );
}
Geek Happy

Wow, that's so cool! You can combine components to build more complex UIs!

Gal Happy

You got it! That's the power of React components! 💪

  • Conclusion React components are reusable pieces of your app’s user interface. They can be written as JavaScript functions or classes, and you can pass them props to customize their appearance or behavior. By composing components, you can create complex UIs while keeping your code clean and maintainable! 😊