~/reactjs/reactjs-how-create-components.md

ReactJS - How to create ReactJS components

2 min readJune 27, 2020by Gorav Singal#reactjs
// 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.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

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

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…