reactjs|June 27, 2020|2 min read

ReactJS - How to create ReactJS components

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.

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

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…