Member-only story
React JS — Understanding Functional & Class Components
React JS is a popular JavaScript library used for building user interfaces. One of the key concepts in React is components, which are reusable and self-contained pieces of code that define the structure and behaviour of a specific part of a UI. When working with React, you’ll often come across two types of components: functional components and class components. In this article, we’ll explore these two types of components in depth, discussing their differences, and use cases, and providing examples to help you understand them better.
Functional Components:
Functional components are the simplest form of components in React. They are also referred to as stateless components because they don’t have their own internal state. Functional components are defined as JavaScript functions that accept props as input and return JSX (JavaScript XML) as output.
Let’s consider an example of a functional component that displays a simple “Hello, World!” message:
import React from 'react';
const HelloWorld = () => {
return <div>Hello, World!</div>;
};
export default HelloWorld;
In the example above, we define a functional component called HelloWorld
. It doesn't have any props, so we don't pass anything inside the parentheses. The component returns JSX…