Logo

Developer learning path

React

Functional Components in React

Functional Components

59

#description

Functional Components in React are a type of component that is created using a JavaScript function. They are the simplest and easy-to-understand way to create a React component, without the need for the class syntax.

Functional components are preferred over class components when we need to write less code, reuse code and handle simple states in a component. Unlike class-based components, functional components do not have their own internal state or lifecycle methods.

Here's an example of a functional component that displays a basic message:

                    
import React from 'react';

function Welcome(props) {
  return <h1>Hello {props.name}!</h1>;
}

export default Welcome;
                  

As you can see, the function takes props as a parameter and returns the desired output, in this case, an h1 tag with a "Hello" message and the name of the user (passed through props).

In functional components, we use React hooks such as useState, useEffect and useContext to introduce some state functionality and manipulate state data.

Functional components are more elegant, concise and easy to read, making them a favorite among developers in the React community.

March 25, 2023

If you don't quite understand a paragraph in the lecture, just click on it and you can ask questions about it.

If you don't understand the whole question, click on the buttons below to get a new version of the explanation, practical examples, or to critique the question itself.