×

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

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 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 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.

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.

Difference Between React Flux and MVC

React Flux vs MVC MVC MVC is an acronym of 'Model View Controller.' It is the architectural pattern that is used to develop the user interface. It has three different logical components: The...

2 minutes read.

React Props Validation

React Props Validation As we know, props are a suitable mechanism for passing the read-only attributes to react components. The props are required to be correctly used in the component. If it is...

5 minutes read.

React Component API

React Component API It is a top-level API. It provides reusability to the code in the application and makes it completely individual. It has several methods for: Creating Elements.Transforming Elements.Fragments. Now, we are explaining the three...

2 minutes read.

React Hooks

React Hooks The Hooks were introduced in React 16.8. By using React Hooks, you can use the state and other features of the React without writing a class. Hooks do not work within the classes....

5 minutes read.

ReactJS vs AngularJS

Difference between ReactJS and AngularJS AngularJS AngularJS is the JavaScript framework that is open source, and it is also used to build a dynamic web application. It is developed in 2009 by...

5 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 Refs

React Refs The word ‘Refs’ is an abbreviation of ‘References’ in React. It is as similar as Keys in React. Refs are a function in React which is used for accessing the DOM element and...

7 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.

React CSS

What is React CSS React CSS is used to provide the style to the React applications. The style attribute adds dynamically-computed styles at render time, and it is one of the most used...

5 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 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 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 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.

React Conditional Rendering

React Conditional Rendering In React, the working of the conditional rendering is similar to the condition works in JavaScript. We use JavaScript operators for creating elements that represent the current state, and then the React...

3 minutes read.

Difference Between State and Props in React

Comparison between State and Props 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...

2 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.