×

React Components

React Components

Former, the developers used to write more than thousands of lines of code to develop a single-page application. These applications follow the structure of traditional DOM, and it was very challenging to make any change with them. And if any mistake found, it requires being search manually in the entire application and update accordingly. The component-based approach is introduced to resolve this issue. According to this approach, the complete application is divided into a small logical group of code, which is known as components.

A component is considered as the core building block of a React application. It makes the task of building UIs convenient. Every component resides in the same space, but works independently from one another and merges all them in a parent component, which will be the final UI of the application.

Every React component has its structure, methods as well as APIs. They can be reused as per the requirements.

There are two types of components in ReactJS that are as follows:

  1. Functional Components (Stateless Components).
  2. Class Components (Stateful component).

Functional Components

The functional components are also known as stateless components because they do not hold or manage state. Functional components are a way to write components that only includes a render method and do not have their state. They simply are the JavaScript functions that might or might not receive the data as parameters. We also can create a function that has props (properties) as inputs and returns what should be rendered.

The example of Stateless components is as follows:

Example

import React, { Component } from 'react';  
 class App extends React.Component {  
    render() {  
       return (  
          
                                      
        );     }  }  class First extends React.Component {     render() {        return (          
             

Stateless Components

          
        );     }  }  class Second extends React.Component {     render() {        return (          
             

www.tutorialandexample.com

             

This website has several tutorials.

          
        );     }  }  export default App; 

Output:

Functional Components

Class Components

Class components are also called as Stateful Components, because of holding or managing local state. These components are more complex than stateless components. It requires you to extend from React.

The Class can be created by defining a class that extends the component and has a render function.

The example of the class component is as follows:

Example:

In this example, we will create the list of unordered elements, where we will insert Name dynamically for every object from the data array. Here, we use EcmaScript6 arrow syntax (=>) that looks much cleaner than the old JavaScript syntax. It helps us in creating our elements with fewer lines of code. It is useful when we require creating a lot of item list.

import React, { Component } from 'react';  
 class App extends React.Component {  
  constructor() {  
       super();  
       this.state = {  
          data:   
          [  
             {             
                "name":"Arun" 
             },  
             {            
                "name":"Anup"             
             },  
             {    
                "name":"Anil"          
             }  
          ]  
       }  
    }  
    render() {  
       return (  
          
                           
                                {this.state.data.map((item) => )}                      
          
        );     }  }  class Name extends React.Component {     render() {        return (          
             

Student Names

          
        );     }  }  class List extends React.Component {     render() {        return (          
                           
  • {this.props.data.name}
  •             
        );     }  }  export default App;

Output:

ReactJs Class Components

Related Topics

React Redux

How to Use Redux with React Hooks It is an open-source library of JavaScript which is used for managing the application state. React uses the Redux to build the user interface....

5 minutes read.

React State

React State The state is an updatable structure which is used for containing data or information about the component. The state in a component can be changed over time. The change...

3 minutes read.

React Fragments

React Fragments The fragments in React are introduced from the 16.2 and above versions. Fragments allow you to group a list of child elements without adding any extra node in the DOM. In React,...

2 minutes read.

React Error Boundaries

React Error Boundaries In the past, the errors of JavaScript within the components were used to corrupt the internal state of react and cause it to emit the cryptic errors on the next...

3 minutes read.

React Constructors

What is constructor? The constructor is a method that is used for initializing the state of the object in the class. It calls automatically during the creation of an object in...

4 minutes read.

React Native vs ReactJS

Difference between React Native and ReactJS React Native React Native is also an open-source JavaScript framework that is used for the development of a mobile application for iOS Android and Windows. It...

6 minutes read.

ReactJS Versions

ReactJS Versions The Complete set of release history of ReactJS is elaborated as follows. The complete set of full documentation of recent releases is on GitHub. S.noVersionRelease DateExplanation1.0.3.029/05/2013Initially released for public.2.0.4.029/07/2013Supporting comments...

4 minutes read.

React Code Splitting

React Code Splitting React Code Splitting is the procedure of splitting the bundle files by which the files get easily loaded on the webpage. The react application bundles the files by using...

4 minutes read.

React Events

React Events An event is an action that triggers a response of the user action or system-generated event. As HTML, React can also perform actions based on user events. React has the...

2 minutes read.

React Forms

React Forms Forms are the important part of any web application. It allows the interaction of the user with the app as well as the gathering of the information from the...

3 minutes read.

React Keys

React Keys A key can be defined as a unique identifier. In React, it is used for identifying the items that have changed, deleted, and updated from the lists. React Keys are helpful...

3 minutes read.

React Table

React Table The table represents an arrangement that organizes the information in the form of rows and columns. The table is mainly used for storing and displaying the data within a structured format. Features of...

2 minutes read.

React JSX

React JSX As we know, all of the React components include a render function. The Render function specifies the HTML output of a React component. JSX is an acronym of JavaScript extension that...

3 minutes read.

Difference between Controlled and Uncontrolled component in ReactJs

Controlled Component In the controlled component, the input of the form element is handled by the component rather than the DOM. Controlled components have the functions that govern the data, which...

2 minutes read.

Pros and Cons of ReactJS

Pros and Cons of ReactJS There are various advantages, and disadvantages of ReactJS are as follows: Benefits of ReactJS 1. Easy to Learn and Use: ReactJS is very easy to use and learn. It has a good...

3 minutes read.

React Map

React Map The map() is the standard function of JavaScript, which can be called at any array. The map() method is used for traversing and displaying a list of the similar objects of...

2 minutes read.

ReactJS Tutorial

ReactJS Introduction React Tutorial helps you to understand the basic and some advanced concepts of ReactJS. As of now, it is the essential front-end library that is developed by a...

3 minutes read.

React Bootstrap

React Bootstrap React has a widely used JavaScript framework for creating web applications, and Bootstrap has become the most popular CSS framework. Single-page apps have become popular from the last few...

9 minutes read.

React Router

What is React Router? Routing is a process by which a user can direct to different pages based on their actions and requests. ReactJS router is generally used to develop single...

10 minutes read.

Comparison between React and Vue

Comparison between React and Vue React, and Vue both are the two most famous libraries of JavaScript that are used for creating thousands of websites today. The React and Vue both...

3 minutes read.