Ads Top

React Basics: Types Of Components

Read

What is a Component in React?

From the Facebook documentation: Components let you split the UI into independent, reusable pieces, and think about each piece in isolation. Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called "props") and return React elements describing what should appear on the screen.

As plainly stated above, one of the main advantages of components is taking potentially large pieces of code that make up views and break them down in the smaller more manageable pieces that are easier to work with.

Functional Component

Some info comes in (props), and some JSX comes out (being displayed). Notice how the syntax looks. It is a standard Javascript function.

Class Component

A component that needs to be aware of its own state
In React, when we refer to state, we are saying a plain javascript object used to record and react to user events.

Each class based component we define has it’s own state based object. Whenever a component state is changed, the component immediately rerenders. It will also rerender its child components Before you use state inside a component, you need to initialize the state object.

To set the property state, to a plain js object inside of the class constructor method.

While trivial, the following example makes it easy to follow the basics of a class component.

A few key points
1: class Welcome extends Component: This is how you define an ES6 class based component. When declaring, you need to have the following import at the top of your file.

import React, { Component } from 'react';

Destructuring { Component } from react allows you to define the class like this

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. ES6 Training in Chennai