Questions tagged [react-functional-component]

Function components in React are plain JavaScript functions that are used as React components. They have access to component features via Hooks. Use this tag for questions regarding the usage or behavior of function components. Do not use this tag if the question just uses them, but doesn't specifically asks about them.

Function components (misleadingly also known as "functional" components or stateless components) are JavaScript functions that are used as components. They are essentially render functions (their return value determines what the component renders).

They receive props as an argument.

This is an example of a function component:

function HelloWorld({ color }) {
  return <div style={{ color }}>Hello World!</div>;
}

ReactDOM.render(
  <HelloWorld color="blue" />, 
  document.body
);

Function components are called stateless because they don't have a component instance bound to them, but with the addition of Hooks, they also have access to state, commit-phase side effects, etc.

Because Hooks can't be used with class components, function components have slightly different features and use-cases, and they are not always interchangeable with class components.

Official documentation: Components and Props

1301 questions
238
votes
12 answers

How to force a functional React component to render?

I have a function component, and I want to force it to re-render. How can I do so? Since there's no instance this, I cannot call this.forceUpdate().
Gorakh Nath
  • 9,140
  • 15
  • 46
  • 68
202
votes
5 answers

TypeScript React.FC confusion

I am learning TypeScript and some bits are confusing to me. One bit is below: interface Props { name: string; } const PrintName: React.FC = (props) => { return (

Kuldeep Bora
  • 4,072
  • 3
  • 12
  • 16
175
votes
4 answers

Using async/await inside a React functional component

I'm just beginning to use React for a project, and am really struggling with incorporating async/await functionality into one of my components. I have an asynchronous function called fetchKey that goes and gets an access key from an API I am…
50
votes
3 answers

React - useRef with TypeScript and functional component

I'm trying to call the child component method from the parent component using useRef. In the future, the SayHi method will update the hook state in the child component. Unfortunately, I have bugs I can't deal with. Line:…
Adam Nowicki
  • 504
  • 1
  • 4
  • 9
27
votes
7 answers

Uncaught ReferenceError: regeneratorRuntime is not defined in React

I'm getting an error "Uncaught ReferenceError: regeneratorRuntime is not defined". Please help me to find out the error and how to resolve it.
Abhijeet
  • 588
  • 1
  • 4
  • 18
16
votes
3 answers

Empty Dependencies with useMemo or useCallback VS useRef

In this GitHub issue I essentially proposed changing: x = useCallback( ... , []); To: x = useRef( ... ).current; The two are the same but with useRef React doesn't compare the dependencies. For which a reply came with a question: Is there ever a…
Izhaki
  • 23,372
  • 9
  • 69
  • 107
13
votes
1 answer

How to test methods inside functional component using enzyme as instance() returns null for shallow wrapper?

Let's say I have a simple component like this. import React, { useState } from "react"; const Counter = () => { const [counter, setCounter] = useState(0); const incCounter = () => { setCounter(counter + 1); }; return ( …
11
votes
1 answer

Is it bad practice to return a JSX element from a React hook?

I've written the following hook for alert boxes: import MuiAlert from '@material-ui/lab/Alert'; import { Snackbar } from '@material-ui/core'; import React from 'react'; export const useAlert = () => { const [open, setOpen] =…
11
votes
2 answers

Get multiple URL parameters using useParams() hook

I am trying to pass multiple parameters in a url using React-Router 5.1 and the useParams hook in a Functional component. However I encounter really strange behavior. I paste the url into the client. Url: …
10
votes
2 answers

componentWillMount for react functional component?

For react class component, we have the componentWillMount() lifecycle method, where we can perform tasks before loading the component. Tasks like, a call to backend and use the response to show that data in that frontend. What if I want the same…
kushal verma
  • 135
  • 1
  • 2
  • 11
10
votes
1 answer

React functional component - using inline functions for handlers affects performance?

When using class components it was always recommended to never do inline anonymous functions as its bad for performance i.e. this.setState({title: event.target.value})} /> You generally…
Martin
  • 501
  • 1
  • 4
  • 10
8
votes
2 answers

How to create a reusable input field using react?

I have InputField component which accepts various props like type,placeholder,value, etc. i am trying to create a form using InputField component. I can easily pass props from form but i cant save input values to my state. Here is my…
user3869304
  • 853
  • 2
  • 11
  • 23
8
votes
3 answers

FluentUI DetailsList onColumnClick with React Hooks gives empty Items

I am trying to create a DetailsList with sortable columns (similar as the example in the documentation here: https://uifabric.azurewebsites.net/#/controls/web/detailslist), but instead of a Class component I want to use a functional components and…
8
votes
2 answers

Call API Every X Seconds in React Function Component

I have the following react class component to call an API every 10 seconds. Its works with no issues. class Alerts extends Component { constructor() { this.state = { alerts: {}, } } componentDidMount() { this.getAlerts() …
7
votes
2 answers

How to pass particular property to all child components in react?

I am absolute beginner in react. I have to pass particular property to all the components inside the div without passing them into individual, For Example, Instead of doing this: function App() { return (
1
2 3
86 87