What is functional component in React?

Experience Level: Junior
Tags: React

Answer

A functional component in React is a type of component that is defined as a JavaScript function. It is a simpler way to define a component compared to a class component. Functional components are stateless, meaning they do not have their own state and do not use lifecycle methods. They are used to display data and receive props from a parent component. Functional components are useful for creating reusable UI elements that do not require complex logic or state management. They are also faster to render compared to class components because they have a smaller memory footprint. Here is an example of a functional component that displays a list of items passed down as props:


function ItemList(props) {
  return (
    <ul>
      {props.items.map(item => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
}

In this example, the ItemList component receives an array of items as props and maps over them to display each item as a list item. The key prop is used to uniquely identify each item in the list. Functional components can also use React hooks to manage state and lifecycle methods. The useState hook is used to add state to a functional component. Here is an example of a functional component that uses the useState hook to toggle a button:


import React, { useState } from 'react';

function ToggleButton() {
  const [isOn, setIsOn] = useState(false);

  function handleClick() {
    setIsOn(!isOn);
  }

  return (
    <button onClick={handleClick}>
      {isOn ? 'ON' : 'OFF'}
    </button>
  );
}

In this example, the ToggleButton component uses the useState hook to add a boolean state value called isOn and a function to update it called setIsOn. The handleClick function is called when the button is clicked and toggles the isOn state value. The button text is updated based on the isOn state value. Overall, functional components are a powerful tool in React for creating reusable UI elements and managing state and lifecycle methods using hooks. They are a simpler and faster alternative to class components for components that do not require complex logic or state management.

React for beginners
React for beginners

Are you learning React ? Try our test we designed to help you progress faster.

Test yourself

Chat

Oh, the operator is not available. Leave us your comments. We will answer all your questions as soon as possible.

Comments

RiceHawk18
e
RiceHawk18
@@xeDO0
RiceHawk18
1'"
RiceHawk18
e'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
RiceHawk18
L7oVYP7m')) OR 312=(SELECT 312 FROM PG_SLEEP(15))--
RiceHawk18
A1v25QPv') OR 393=(SELECT 393 FROM PG_SLEEP(15))--
RiceHawk18
kxT46vOm' OR 479=(SELECT 479 FROM PG_SLEEP(15))--
RiceHawk18
VTgcz37T'; waitfor delay '0:0:15' --
RiceHawk18
1 waitfor delay '0:0:15' --
RiceHawk18
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
RiceHawk18
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
RiceHawk18
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
RiceHawk18
if(now()=sysdate(),sleep(15),0)
RiceHawk18
-1" OR 3+906-906-1=0+0+0+1 --
RiceHawk18
-1" OR 2+906-906-1=0+0+0+1 --
RiceHawk18
-1' OR 3+316-316-1=0+0+0+1 or '8BoDIAd6'='
RiceHawk18
-1' OR 2+316-316-1=0+0+0+1 or '8BoDIAd6'='
RiceHawk18
-1' OR 3+137-137-1=0+0+0+1 --
RiceHawk18
-1' OR 2+137-137-1=0+0+0+1 --
RiceHawk18
-1 OR 3+877-877-1=0+0+0+1
RiceHawk18
-1 OR 2+877-877-1=0+0+0+1
RiceHawk18
-1 OR 3+418-418-1=0+0+0+1 --
RiceHawk18
-1 OR 2+418-418-1=0+0+0+1 --
RiceHawk18
e
RiceHawk18
e