Mapagam
  • JavaScript 
  • Web APIs 
  • TypeScript 
  • React 

How to Create Your First React Component (With Example Code)

Posted on April 12, 2025 • 6 min read • 1,149 words
Share via
Mapagam
Link copied to clipboard

Learn how to create your first React component with a step-by-step guide and example code. Start building dynamic UIs in React today!

On this page
1. Introduction to React Components 1.1 What are React Components? 1.2 Types of React Components 2. Setting Up a React Environment 2.1 Prerequisites 2.2 Setting Up a React Project 3. Understanding the Structure of a React Application 4. Creating Your First React Component 4.1 Creating a Functional Component 4.2 Rendering the Component 5. Understanding Props in React 5.1 Using Props in a Functional Component 5.2 Why Props are Important 6. Handling State in React Components 6.1 Using useState to Manage State 7. Conclusion

React is one of the most popular JavaScript libraries for building user interfaces, particularly single-page applications where you need a dynamic, fast, and scalable interface. React’s primary building block is the component, which allows you to create reusable, modular pieces of code that can be rendered to the screen.

1. Introduction to React Components

Before we dive into the process of creating a React component, let’s first understand what components are and why they are central to React development.

1.1 What are React Components?

A React component is a JavaScript function or class that optionally accepts inputs (called props) and returns a React element that describes how a section of the UI should appear. The UI is typically rendered using JSX (JavaScript XML), a syntax extension that allows you to write HTML-like code within JavaScript.

Components help to break down the user interface into smaller, manageable pieces, each handling a specific task, which makes React-based applications highly scalable and easy to maintain.

1.2 Types of React Components

React components can be broadly categorized into two types:

  • Functional Components
  • Class Components

For this tutorial, we’ll focus on functional components because they are simpler and the recommended approach for building components in React, especially with the advent of React hooks.

2. Setting Up a React Environment

Before you start creating components, you need to have React set up in your development environment. If you don’t have React installed yet, follow these steps.

2.1 Prerequisites

  • Node.js and npm: React requires Node.js to be installed on your machine. npm (Node Package Manager) comes bundled with Node.js and is used to install packages, including React.

  • Text Editor: Choose a code editor like VS Code, Sublime Text, or Atom to write your React code.

2.2 Setting Up a React Project

The easiest way to set up a React project is by using Create React App. Create React App is a command-line tool that sets up a new React project with a sensible default configuration.

  1. First, open your terminal or command prompt.
  2. Install create-react-app globally (if you haven’t already):
npm install -g create-react-app
  1. Now, create a new React application:
npx create-react-app my-first-react-app
  1. After the process finishes, navigate to the newly created directory:
cd my-first-react-app
  1. Start the development server:
npm start

This should open the application in your browser at http://localhost:3000. Now you’re all set to start creating components!

3. Understanding the Structure of a React Application

When you create a React application using Create React App, you’ll see several folders and files. Let’s take a quick look at the important ones.

  • public/index.html: This is the HTML file that serves as the entry point for your app. The <div id="root"></div> element is where React will inject your components.
  • src/index.js: This is the JavaScript file where your React application is bootstrapped. The ReactDOM.render method renders the root component to the DOM.
  • src/App.js: This is the default component that gets rendered in the browser when you first run your app.

4. Creating Your First React Component

Now, let’s create a simple React component. We will start with a basic component that displays “Hello, World!” on the screen.

4.1 Creating a Functional Component

In the src directory, create a new file called HelloWorld.js. This will contain our first functional React component.

Here’s the code for the HelloWorld.js component:

import React from 'react';

function HelloWorld() {
  return <h1>Hello, World!</h1>;
}

export default HelloWorld;

4.1.1 Code Explanation

  • Importing React: Every React component must import the React object from the react library. This is necessary because JSX is transformed into React.createElement calls under the hood.

  • Functional Component: A functional component is simply a JavaScript function that returns JSX. In our example, HelloWorld is the function that returns the JSX <h1>Hello, World!</h1>.

  • Exporting the Component: To make the component reusable, you need to export it using export default HelloWorld. This allows other parts of your app to import and use this component.

4.2 Rendering the Component

Now that you’ve created your HelloWorld component, it’s time to render it to the screen.

  1. Open the src/App.js file.
  2. Import the HelloWorld component at the top of the file:
import HelloWorld from './HelloWorld';
  1. Replace the contents of the App component with HelloWorld:
function App() {
  return (
    <div className="App">
      <HelloWorld />
    </div>
  );
}

Now, if you navigate to your browser (typically at http://localhost:3000), you should see the text “Hello, World!” displayed on the page.

5. Understanding Props in React

One of the powerful features of React is props (short for properties), which allow components to accept dynamic values. Props help to make components reusable with different data.

5.1 Using Props in a Functional Component

Let’s modify the HelloWorld component to accept a name via props.

5.1.1 Updated Code for HelloWorld.js

import React from 'react';

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

export default HelloWorld;

5.1.2 Code Explanation

  • Receiving Props: We modified the HelloWorld component to accept props as an argument. Inside the JSX, we use props.name to display the name passed from the parent component.

5.1.3 Passing Props from App.js

Now, in App.js, we can pass a name to the HelloWorld component:

function App() {
  return (
    <div className="App">
      <HelloWorld name="John" />
    </div>
  );
}

This will render “Hello, John!” in the browser.

5.2 Why Props are Important

Props allow you to make your components flexible. Instead of hardcoding values inside a component, you can pass different data each time the component is used. This is crucial for building dynamic user interfaces.

6. Handling State in React Components

While props are used to pass data into components, state is used to store and manage data within a component. React provides a hook called useState that allows you to add state to functional components.

6.1 Using useState to Manage State

Let’s modify the HelloWorld component to allow the name to be changed dynamically.

6.1.1 Updated Code for HelloWorld.js

import React, { useState } from 'react';

function HelloWorld() {
  const [name, setName] = useState('John');

  const handleChangeName = () => {
    setName('Jane');
  };

  return (
    <div>
      <h1>Hello, {name}!</h1>
      <button onClick={handleChangeName}>Change Name</button>
    </div>
  );
}

export default HelloWorld;

6.1.2 Code Explanation

  • useState: The useState hook is used to declare a state variable (name) and a function (setName) to update the state. Initially, the name is set to 'John'.

  • Button and Event Handler: We added a button that calls the handleChangeName function when clicked. This updates the state, causing the component to re-render with the new name.

Now, when you click the “Change Name” button, the text will change from “Hello, John!” to “Hello, Jane!”

7. Conclusion

Congratulations! You’ve just created your first React component. You learned how to:

  • Set up a React project.
  • Create a functional component.
  • Pass data using props.
  • Manage component state with useState.

React components are the foundation of React development, and understanding them will help you build scalable and maintainable applications. Keep experimenting with components, props, and state to master React and take your development skills to the next level.

React Components   Create React Component   React Tutorial   Functional Components   React for Beginners  
React Components   Create React Component   React Tutorial   Functional Components   React for Beginners  
 React Lists and Keys Explained (Why You Should Use Them)
React useEffect Hook Explained for Beginners (With Examples) 

More Reading!

  1. React State vs. Props: Understanding the Key Differences
  2. React Event Handling: A Beginner’s Guide to onClick, onChange & More
  3. How to Use the useState Hook in React (With Simple Examples)
  4. React Components Explained: The Ultimate Beginner’s Guide
On this page:
1. Introduction to React Components 1.1 What are React Components? 1.2 Types of React Components 2. Setting Up a React Environment 2.1 Prerequisites 2.2 Setting Up a React Project 3. Understanding the Structure of a React Application 4. Creating Your First React Component 4.1 Creating a Functional Component 4.2 Rendering the Component 5. Understanding Props in React 5.1 Using Props in a Functional Component 5.2 Why Props are Important 6. Handling State in React Components 6.1 Using useState to Manage State 7. Conclusion
Follow me

I work on everything coding and technology

   
Mapagam
Mapagam is your go-to resource for all things related to frontend development. From the latest frameworks and libraries to tips, tutorials, and best practices, we dive deep into the ever-evolving world of web technologies.
Licensed under Creative Commons (CC BY-NC-SA 4.0).
 
Frontend
JavaScript 
Web Api 
TypeScript 
React 
Social
Linkedin 
Github 
Mapagam
Code copied to clipboard