Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

ReactJS Class Based Components

  • Difficulty Level : Expert
  • Last Updated : 04 Feb, 2021

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:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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 distinguish them from functional components is that they have access to a state which dictates the current behavior and appearance of the component. 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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!