Hi there! 👋 Welcome to this detailed and friendly guide on React components. If you're new to React, don't worry—we'll walk you through everything step by step, using simple and easy-to-understand language.
Think of components like building blocks for your website—just like how you can build complex structures with Lego blocks. Each component represents a small part of your website, and once you create a component, you can reuse it multiple times in different places!
For example, in a social media app, you might have:
By breaking your app into smaller components, you make your code easier to manage and maintain!
In React, there are two main types of components:
Ready to build your first React component? Let’s get started step by step!
First, choose a name for your component. One important rule is that the component name must always start with a capital letter. For this example, let's call our component Welcome
.
Now, let's write a simple function. This function will return the content that you want to display on the screen. Here's an example:
function Welcome() {
return <h1>Hello, friend!</h1>;
}
Let's break this down:
function Welcome()
creates a new function called Welcome. This function will generate some content for us.return
tells React what to display on the screen. In this case, we're returning an HTML element—a big heading (<h1>
) that says "Hello, friend!"Whenever we use the Welcome
component, it will display this heading.
To use your component in other parts of your app, you need to export it. Exporting a component is like packing it up in a box so that you can easily move it around and use it anywhere in your project. Here's how you do that:
function Welcome() {
return <h1>Hello, friend!</h1>;
}
export default Welcome;
The export default Welcome;
line makes the Welcome
component available to other files in your app.
Now that we've created the Welcome
component, let's learn how to use it inside our app. We need to do two things:
Here's an example of how to do that:
import React from 'react';
import Welcome from './Welcome'; // Importing the Welcome component
function App() {
return (
<div>
<Welcome /> {/* Using the Welcome component */}
</div>
);
}
export default App;
Here's what's happening:
import React from 'react';
is always needed when writing React components.import Welcome from './Welcome';
brings the Welcome
component we created earlier into this file. The ./
means that the Welcome
component is in the same folder.<Welcome />
is how we use the Welcome
component. This looks similar to an HTML tag but represents the component we created.<Welcome />
Instead of Welcome()
?Great question! In React, we use the syntax <Welcome />
because:
Sometimes, you'll want to return more than one element from your component. For example, maybe you want to return both a heading and a paragraph:
function Welcome() {
return (
<div>
<h1>Hello, friend!</h1>
<p>Welcome to our app!</p>
</div>
);
}
Notice that both the heading and paragraph are inside a <div>
element. This is important because React requires your component to return one single parent element. If you don’t wrap the elements, React will show an error.
Let's create another simple component to practice. This time, we'll make a button component:
function ButtonClick() {
return (
<button>Click me!</button>
);
}
export default ButtonClick;
Now, let's use this new button in our App component alongside the Welcome component:
import React from 'react';
import Welcome from './Welcome';
import ButtonClick from './ButtonClick';
function App() {
return (
<div>
<Welcome />
<ButtonClick />
</div>
);
}
export default App;
Here are the key points to remember:
export default
to make your component available to other parts of your app.import
to bring components into the file where you want to use them.<ComponentName />
in your JSX code.<div>
) when returning them from a component.Keep practicing, and soon you'll be creating amazing things with React components! 😊