DEV Community

Cover image for How to Build a React Component from Scratch: A Gentle Guide
 Tijani Olagunju
Tijani Olagunju

Posted on

How to Build a React Component from Scratch: A Gentle Guide

Welcome to this guide I prepared to walk you through creating a react component from scratch. The only prerequisites you need are basic HTML, CSS, and JavaScript foundational knowledge. Let's get to business...


Table of Contents

  1. Introduction: You’re Not Alone in This
    • A welcoming note to ease apprehension.
  2. What Is a React Component?
    • Understanding the foundation.
  3. Setting Up Your Environment
    • Tools and setup made simple.
  4. Creating Your First Component
    • Step-by-step guide to your first working piece.
  5. Styling the Component
    • Ways to add aesthetic with ease.
  6. Managing State and Props
    • Making your component dynamic.
  7. Best Practices and Troubleshooting
    • Tips to write clean, resilient code.
  8. Conclusion: You Did It!
    • A reflection and encouragement for what comes next.

1. Introduction: You’re Not Alone in This

Learning to build a React component from scratch can feel like stepping into a vast and unfamiliar world. If you’ve found yourself unsure where to start—or frustrated because things don’t “just work”—please know this: you are not alone. Every expert React developer was once a beginner. This guide is written to hold your hand through the process, no assumptions, no judgement—just encouragement and clarity.


2. What Is a React Component?

A React component is the building block of a React application. Think of it as a modular piece of UI—a button, a card, a form field—that encapsulates logic, layout, and style. There are two main types:

  • Function Components: Modern, simpler, and the current best practice.
  • Class Components: Older, more verbose, but still used in legacy codebases.

We’ll focus on function components because they’re simpler and more intuitive for newcomers.


3. Setting Up Your Environment

Before writing any code, let’s ensure your development environment is ready:

What You Need:

  • Node.js and npm: You can download them from nodejs.org.
  • Code Editor: Visual Studio Code is widely used and beginner-friendly.
  • Create React App: An official starter pack to set up your project fast.

Steps:

  1. Open your terminal.
  2. Run:
    npx create-react-app my-first-component
    cd my-first-component
    npm start
Enter fullscreen mode Exit fullscreen mode

3.Your browser should open with the default React app—success!

If anything feels overwhelming at this point, pause. Take a deep breath. You’re doing just fine.


4. Creating Your First Component

Let’s create a simple Greeting component that says hello to a user.

Step-by-Step:

  1. Inside your project’s src/ folder, create a new file: Greeting.js

  2. Add the following code:


import React from 'react';

function Greeting() {
  return <h1>Hello, welcome to your first component!</h1>;
}

export default Greeting;
Enter fullscreen mode Exit fullscreen mode

3.In App.js, import and use it:

    import Greeting from './Greeting';

    function App() {
      return (
        <div>
          <Greeting />
        </div>
      );
    }
Enter fullscreen mode Exit fullscreen mode

Why This Works:

  • We define a function named Greeting that returns JSX( a syntax extension that looks like HTML).
  • We export it so it can be used in other parts of the app.
  • We import and include <Greeting />in our main app file.

Congratulations—you’ve just built a React component!


5. Styling the Component

Styling helps make your UI approachable and personal.

Inline Styling:

return <h1 style={{ color: 'blue' }}>Hello, styled component!</h1>;
Enter fullscreen mode Exit fullscreen mode

Using CSS Files:

  1. Create Greeting.css:
.greeting-text {
  color: teal;
  font-size: 24px;
}
Enter fullscreen mode Exit fullscreen mode

2.Modify Greeting.js:


    import './Greeting.css';

    function Greeting() {
      return <h1 className="greeting-text">Hello with CSS!</h1>;
    }
Enter fullscreen mode Exit fullscreen mode

Both options are valid—choose what feels manageable for you as you learn.


6. Managing State and Props

Now let’s add props to personalize the message and state to make it interactive.

With Props:

function Greeting({ name }) {
  return <h1>Hello, {name}!</h1>;
}

// Use in App.js
<Greeting name="Alex" />
Enter fullscreen mode Exit fullscreen mode

With State:

import React, { useState } from 'react';

function Greeting() {
  const [name, setName] = useState('Alex');

  return (
    <>
      <h1>Hello, {name}!</h1>
      <button onClick={() => setName('Taylor')}>Change Name</button>
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

With useState, React helps you keep track of values that can change—like a name after a button click. If this feels like a leap, it’s okay. Repetition and small experiments will make it second nature.


7. Best Practices and Troubleshooting

Keep in Mind:

  • Name Components with Capitals: React treats lowercase tags like built-in HTML.
  • Return One Root Element: Wrap JSX in a single parent, like < div> or <>.
  • Use Meaningful Names: Components like UserCard, Header, LoginForm tell their own story.

When Things Break:

  • Read Error Messages Slowly: Often they guide you to the exact line.

  • Restart the Server: Sometimes Ctrl + C and npm start again solves mysterious issues.

  • Google the Error: Every error you’ll meet has likely been solved publicly already.


8. Conclusion: You Did It!

You’ve written your first component, styled it, passed in props, used state, and survived a few quirks along the way. That’s not just technical progress—it’s emotional growth, too. Learning to code is as much about confidence as it is about syntax.

Whether you're still unsure or feeling victorious, this is a huge milestone. Take a break, hydrate, and remember: you’re building something powerful, one component at a time.

For additional resources on YouTube Check out these tutorials:@programming with mosh --React Tutorial for Beginners-- @useful programmer --Write a React Component from Scratch - React - Free Code Camp--.
Follow me for more updates and type in your questions and feedback in the Comment box.

With that we come to the end of this guide.

Top comments (0)