Class Components
React allows us to define components as class or function components. Class components that are stateful can be created by extending React.Component
and are more feature-rich than function components and provide us with a number of predefined methods that make managing data and components easy. We can also write our own methods that can be used at different stages of the component.
Let's see an example of a simple class component that shows a Hello World
message:
import React, { Component } from "react"; class HelloMessage extends Component { Â Â render() { Â Â Â Â return <h1>Hello World!</h1>; Â Â } } export default HelloMessage;
This can be used in a file as:
import HelloMessage from "./HelloMessage";
If you've noticed, we have used a syntax that may seem unfamiliar to developers who are new to React. React code is usually written in ECMAScript 2015 or later. ECMAScript 2015...