What is a React Component?

What is a React Component?

Written by krissanawat101 on Dec 28th, 2020 Views Report Post

React is one of the most popular JavaScript frameworks available. It is a powerful and light-weight library for building large-scale web applications that can update the DOM without having to reload the page. The core objective of React is to make web development simple, fast, and scalable.

One of the main features of React is its component-based approach towards development. Components are the backbone of every React application. In this article, we are going to learn the basics of React components.

What is a component

At its core, a component is essentially a function that takes in properties and returns a template. This component is a composition of HTML, CSS and JavaScript. Here is the simplest form of a functional component:

cost App = (props) => {
	return(
		<div>A Functional Component</div>
	);
}

The code above shows the basic form of a functional component called App. It accepts properties (props) from the parent component and renders out an HTML template.

There are many ways to create a component in React. In this tutorial, I will show you the two most popular ways to build a React component, which are Class-based components and Functional components.

Class-based components

Components written with the ES6 Class syntax, instead of functions, are referred to as a Class-based component. Class-based components are the traditional way of formulating components in React. They are stateful, which means they hold the state of the data and the template. The class component can re-render the data as the state is changed.

These component classes have a constructor function called every time the component is rendered to the DOM and they have access to React life cycle hooks such as componentDidMount, componentDidUpdate, componentDidUnmount etc. These lifecycle hooks help form the logical entities of a class component. They can be used to control the functionality of the component.

Here is a typical example of a Class component:

import React from 'react';

class App extends React.Component {
    constructor(props) {
        super(props);       
    }
    render() {
        return (
            <div>{this.props.name}</div>
        );
    }
}

export default App;

Above, we have a simple class component called App that extends the React.Component module. This means that the component inherits all the functionality of the React component module.

The constructor() method is automatically called every time the component is created in the DOM. This gives us the ability to perform any initialization tasks required to make the object useful. This gets called once as the component is rendered and cannot be programmatically called again. Then, it calls a super method, which means that this component calls the constructor of its parent component whenever rendered in the DOM.

Next, we have props, which are the properties passed down from the parent components. We can use these to perform logical operations or even render out data in the child component.

Finally, the render() function will return our HTML template, rendering the state and prop values along with the template.

That is the basics of creating a simple React Class-based component. Next, we'll talk about Functional components.

Functional Components

A functional component is a new way of representing a component in the React ecosystem. With functional components, we define components as a function.

The overall pattern of defining the function is similar to that of the traditional class-based components; however, instead of using class methods we use a simple function.

These type of components may be referred to as a stateless component because they do not contain state variables. Hence, there are no logical operations or dynamic data binding happening in the functional component. It is simply for UI and display purposes. The main advantage of functional components is to separate the template from the logic.

A simple example of a functional component is provided in the code snippet below:

const App = (props) => {
  return (
    <p>This is functional component: {props.name} </p>
  )
}

The functional component doesn't have access to this object, which means that the component is independent. When it is rendered, each component is a new instance of ReactComponent. It has no communication with the global object or any adjacent child components. This means that we cannot apply any state or lifecycle hook functionality. That is until we had React Hooks.

With the introduction of React Hooks, we are now able to apply the state inside the functional component. Using React Hooks such as useState and useEffect we can create dynamic data, and re-render our template when the data is updated. This gives us more power when using functional components, which is why they are more widely used nowadays.

This makes functional components easier to maintain without the heavy lifting of methods and lifecycle hooks.

Advantages of Component-based Development in React

There are many prominent benefits of using components as a whole. They form a new way of structuring the overall project with clarity and cleanliness. Here are some of the advantages of taking a component-based development approach:

  • Components help separate large chunks of code into smaller units with their own individual existence in the DOM.
  • The overall code becomes easier to read, easier to understand, and easier to test.
  • Functional components help reduce the number of lines of code and enhance the performance of our web app.
  • Components make the coding implementation modular and can be re-used multiple times.
  • Component-based development is easier to maintain. They are easier to troubleshoot and update.
  • They help with faster development and optimize the modeling of the overall system.
  • It makes the team workflow in any project easier and more sustainable without conflicts.

Parting Words

Component-driven development is the most prominent way of creating web applications nowadays. Components are the heart and soul of the React ecosystem as they formulate the user interface as well as the underlying logic. It is better to go for functional components at present because it has been equipped with better coding facilities and easier workflows.

React is still developing and creating new ways to make components smarter and simpler.

Components are the way to go nowadays as they give more simplicity to the coding environment and the overall project. The state management tools and higher-order functions have been designed to influence both class components and functional components. The React Hooks are powerful enough to make the functional component act like class components with fewer coding implementations. This has created a new way of structuring the overall component-based development.

A component-based approach is ideal for creating maintainable, scalable, and reusable code.

Comments (0)