ReactJS Class Based Components

10 Jan, 2022
React class based components are the bread and butter of most modern web apps built in ReactJS. These components are simple classes (made up of multiple functions that add functionality to the application). All class based components are child classes for the Component class of ReactJS.  
Example: Program to demonstrate the creation of class-based components. Create a React app and edit the App.js as:
Filename- App.js:
javascript
import React from "react"; class App extends React.Component {  render() {    return <h1>GeeksForGeeks</h1>;  }} export default App;
 Output: 
Once a component is declared, it can be used in other components. Program to demonstrate the use of class based components in other components. 
Example: Open the App.js file and replace the code with the below code.
javascript
import React from "react"; class Sample extends React.Component {  render() {    return <h1>A Computer Science Portal For Geeks</h1>;  }} class App extends React.Component {  render() {    return <Sample />;  }} export default App;
Output: 
The main feature of class-based components that distinguished them from functional components is that they have access to a state which dictates the current behavior and appearance of the component (Later, with React Hooks introduced in version 16.8, we are able to declare a stateful component without declaring a class). This state can be modified by calling the setState() function. One or more variables, arrays, or objects defined as part of the state can be modified at a time with the setState() function. 
Example: Program to demonstrate the use of state in class-based components. Open the App.js file and replace the code with the below code. 
javascript
import React from "react"; class App extends React.Component {  constructor(props) {    super(props);    this.state = { change: true };  }  render() {    return (      <div>        <button          onClick={() => {            this.setState({ change: !this.state.change });          }}        >          Click Here!        </button>        {this.state.change ? (          <h1>Welcome to GeeksforGeeks</h1>        ) : (          <h1>A Computer Science Portal for Geeks</h1>        )}      </div>    );  }} export default App;
Output:
Data is passed to other components with the help of props. Props work similarly for all components in ReactJS be they class based or functional. Props are always passed down from the parent component to the child component. ReactJS does not allow a component to modify its own props as a rule. The only way to modify the props is to change the props being passed from the parent component to the child component. This is generally done by passing a reference to a function in the parent component, which changes the props being passed to the child component. 
Example: Program to demonstrate the use of props in class-based components. Open the App.js file and replace the code with the below code. 
javascript
import React from "react"; class App extends React.Component {  render() {    return <h1>{this.props.data}</h1>;  }} class propsExample extends React.Component {  constructor(props) {    super(props);    this.state = { change: true };  }  render() {    return (      <div>        <button          onClick={() => {            this.setState({ change: !this.state.change });          }}        >          Click Here!        </button>        {this.state.change ? (          <Pass data="Welcome to GeeksforGeeks" />        ) : (          <Pass data="A Computer Science Portal for Geeks" />        )}      </div>    );  }} export default App;
 Output: 
 Class-based components have access to the lifecycle functions like componentWillMount(), componentDidMount(),componentWillReceiveProps(),componentWillUpdate(), shouldComponentUpdate(),render() and componentWillUnmount();. These lifecycle functions are called at different stages of the lifecycle and are used for a variety of purposes like changing the state or doing some work (like fetching data from an external API). They are also referred to as lifecycle hooks.  
Example: Program to demonstrate the use of lifecycle hooks. Open the App.js file and replace the code with the below code.
javascript
import React from "react"; class App extends React.Component {  constructor(props) {    super(props);    this.state = { text: "Welcome!" };  }   componentWillMount() {    this.setState({      text: "GeeksforGeeks",    });  }   render() {    return <h1>{this.state.text}</h1>;  }} export default App;
 Output: 
Class-based components are slightly slower than their functional counterparts. The difference is very small and is almost negligible for smaller web apps – though the performance difference increases when the number of components in the app increases. Moreover, class-based components involve a lot more coding on the programmer’s part, making them slightly more inefficient to use.
 
shubhamyadav4
ksgowdashivani
JavaScript
ReactJS
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Roadmap to Learn JavaScript For Beginners
Difference Between PUT and PATCH Request
How to fetch data from an API in ReactJS ?
Axios in React: A Guide for Beginners
How to redirect to another page in ReactJS ?
ReactJS Functional Components