Articles → REACT.JS → Class And Function Components In React
Class And Function Components In React
What Are Class Components?
import React, { Component } from 'react';
import ReactDOM from 'react-dom'
class Message extends Component {
fruits = ['Banana', 'orange', 'Apple'];
render() {
return (
<h1>I am a class component demo</h1>
);
}
}
ReactDOM.render(<Message />, document.getElementById('root'));
Click to Enlarge
What Are Function Components?
import React, { Component } from 'react';
import ReactDOM from 'react-dom'
function Fruits() {
return <h2>Hi, I am a fruit!</h2>;
}
ReactDOM.render(<Fruits/>, document.getElementById('root'));
Click to Enlarge
Difference
- Class component returns HTML using the render method
- Class components may contain variables and functions
- Class components extend the Component class