reactjs2 Min Read

ReactJS - How to create ReactJS components

Gorav Singal

June 27, 2020

TL;DR

Create React components either as simple functions that return JSX or as ES6 classes extending React.Component with a render() method for more complex stateful components.

ReactJS - How to create ReactJS components

Introduction

In this post, we will talk about basic of how to create components in ReactJS. Whatever is rendered through ReactJS is one or more component. Component os the backbones of ReactJS.

There are two ways to create components in ReactJS:

1. Via Function

The simplest way to create a component is via using a function.

import React from 'react'

export default function App() {
    return (
        <div>
            <h1>Hi there</h1>
        </div>
    )
}

Passing Props

To pass props to a functional component, use:

import React, {Component} from 'react';

function Test(prop) {
    return (
        <div>
            <h1>Hi {prop.name}</h1>
        </div>
    )
}

function Test2({desig, name}) {
    return (
        <div>
            <h1>Hi {desig}, {name}</h1>
        </div>
    )
}

function Test3(obj) {
    return (
        <div>
            <h1>Hi {obj.data.desig}, {obj.data.name}</h1>
        </div>
    )
}

class App extends Component {
    render() {
        const data = {id: 1, name: 'Rahul', desig: 'Admin'};
        return <React.Fragment>
            <Test name="Rahul"/>
            <Test2 name="Rahul" desig="Engineer"/>
            <Test3 data={data} />
        </React.Fragment>
    }
}

export default App;

Output:

Hi Rahul
Hi Engineer, Rahul
Hi Admin, Rahul

In above example, I have shown three examples on how you can pass the properties or data or props to the functional component.

2. Via Class

Another way to create component is to have a class inherited through React’s Component class.

import React, { Component } from 'react'

export default class App extends Component {
    render() {
        return (
            <div>
              <H1>Hi</H1>               
            </div>
        )
    }
}

Passing Props

import React, {Component} from 'react';

class Test1 extends Component {
    render() {
        return (
            <div>
                <h1>Hi {this.props.name}</h1>     
            </div>
        )
    }
}
class Test2 extends Component {
    render() {
        return (
            <div>
                <h1>Hi {this.props.person.name} {this.props.person.id}</h1>
            </div>
        )
    }
}

class App extends Component {
    render() {
        const data = {id: 1, name: 'Rahul', desig: 'Admin'};
        return <React.Fragment>
            <Test1 name="Rahul"/>
            <Test2 person={data}/>
        </React.Fragment>
    }
}
export default App;

Output:

Hi Rahul
Hi Rahul 1

Its totally your choice as to how to create components. Traditional javascript developers loves function components. As OOPs programmer loves class components. Although class provides lot of other functionalities like states, lifecycle events etc. Newer version or ReactJS also provides a way to hook states into functional components.

Share

Related Posts

ReactJS - Understanding SetState Asynchronous Behavior and How to correctly use it

ReactJS - Understanding SetState Asynchronous Behavior and How to correctly use it

ReactJS setState is Asynchronous setState() method in ReactJS class components…

ReactJS - How to restrict data type for different kind of data

ReactJS - How to restrict data type for different kind of data

Introduction Javascript is not a strict type language. We can use a variable to…

ReactJS - Basic Form Handling and Form submission

ReactJS - Basic Form Handling and Form submission

Introduction Lets take a look at how forms are being handled in ReactJS. We will…

ReactJS - How to pass method to component and how to pass arguments to method

ReactJS - How to pass method to component and how to pass arguments to method

Introduction In the ReactJS project, you are having aa parent component and a…

ReactJS - 3 ways to Import components in ReactJS

ReactJS - 3 ways to Import components in ReactJS

Introduction In this post, we will discuss 3 different ways to import a…

ReactJS - How to use conditionals in render JSX

ReactJS - How to use conditionals in render JSX

Introduction In this post, I will show several ways to use conditionals while…

Latest Posts

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI Video Generation in 2025 — Models, Costs, and How to Build a Cost-Effective Pipeline

AI video generation went from “cool demo” to “usable in production” in 2024-202…

AI Models in 2025 — Cost, Capabilities, and Which One to Use

AI Models in 2025 — Cost, Capabilities, and Which One to Use

Choosing the right AI model is one of the most impactful decisions you’ll make…

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

AI Image Generation in 2025 — Models, Costs, and How to Optimize Spend

Generating one image with AI costs between $0.002 and $0.12. That might sound…

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

AI Coding Assistants in 2025 — Every Tool Compared, and Which One to Actually Use

Two years ago, AI coding meant one thing: GitHub Copilot autocompleting your…

AI Agents Demystified — It's Just Automation With a Better Brain

AI Agents Demystified — It's Just Automation With a Better Brain

Let’s cut through the noise. If you read Twitter or LinkedIn, you’d think “AI…

Supply Chain Security — Protecting Your Software Pipeline

Supply Chain Security — Protecting Your Software Pipeline

In 2024, a single malicious contributor nearly compromised every Linux system on…