Components
Published Jul 29, 2021Updated Jul 14, 2024
Contribute to Docs
In React, components are pieces of reusable, independent code that make up the user interface (UI) of the application. Each component comes with one or both of the following:
- A state object that contains component data that is expected to change over time.
- A props object with data that can be passed down from parent component to child component.
Class Components
Class components contain a state and use a render()
function to return JSX markup. When defined, the class
has to be an extension of the React.Component
class:
import React from 'react';class ClassComponent extends React.Component {constructor(props) {super(props);this.state = {property1: 'A string',property2: 1,property3: true,};}render() {return <div>...</div>;}}export default ClassComponent;
Function Components
Function components are stateless and only use the return
statement. No import statement necessary:
function FunctionComponent(props) {return <div>...</div>;}
Arrow Function
The arrow function is a feature that was introduced in ES6. It uses the =
and >
characters to define a function like a variable. In React, function components can be defined with this syntax:
const FunctionComponent = (props) => {return <div>...</div>;};
All contributors
- garanews
- Anonymous contributor
- Anonymous contributor
- binarystarr
- christian.dinh
- BrandonDusch
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn React on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn React
In this React course, you’ll build powerful interactive applications with one of the most popular JavaScript libraries.Intermediate13 hours