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
4
votes
4 answers

Is it bad to use React Hook and redux together for React functional component?

I'm using react hook for the local variables such as const [something, setSomthing] = useState('') and redux for storing the variables passed through whole component using store and Provider. But I was told I shouldn't use React Hooks and Redux…
taehoon719
  • 151
  • 2
  • 11
4
votes
1 answer

How to solve closures issue when using React functional component with React.memo?

I am using React functional components alongside React.memo, but I am experiencing an issue, which in my opinion derives from JavaScript closures. (I am also using Immer for immutability, but I believe it does not affect the situation.) Below is a…
4
votes
3 answers

Passing a function as a prop to a Typescript React Functional Component

I have a functional component (written in Typescript) that needs to pass a handler function down to a child component. Here is a scaled down version of the parent function: type Props = { handleLocationChange(): void }; const Sidebar:…
4
votes
1 answer

How to use array state in React function component?

I am trying to use an array state for a React functional component. This is the code I tried. const inputLabel = Array(5).fill(React.useRef(null)); const [labelWidth, setLabelWidth] = React.useState(0); React.useEffect(() => { …
think-serious
  • 1,229
  • 2
  • 12
  • 27
3
votes
2 answers

I am getting 'Uncaught TypeError: props.handleSelect is not a function'

I am trying to create custom components using TailwindCSS and Vite. While passing a function I get the error 'Uncaught TypeError: props.handleSelect is not a function'. const Navbar = () => { const handleSelect = (option) => { option ==…
3
votes
0 answers

Is using inline functions in react still a performance trouble?

I've been using React libraries like MUI for quite a while now. Every time I go through their documentation I see that in almost every place they're defining a separate function even performing a very simple single code of state update. A quick…
3
votes
1 answer

React Native: infinite re-render when setting variable to same value

I'm using a React Native functional component as follows: export const Component1 = () => { const [var1, setVar1] = useState(false); setVar1(false); return ( ) } This goes into an infinite loop, but when I remove the…
3
votes
1 answer

Why am I getting Uncaught TypeError: Cannot read properties of undefined (reading 'name')

I am trying to display a specific beer based on the beer id I am appending to the url import React from "react"; import { useState, useEffect } from "react"; import axios from "axios"; import { Routes, Route, Link, useNavigate, …
3
votes
1 answer

React promised based Confirm Modal doesn't work properly. Once user cancelled, on subsequent modal openings, even if user confirms, it gets cancelled

I am using React Final Form for creating forms. I have a component where I have defined the Confirm Modal Box and promise to handle it. Function implementation is like below: function Campaign() { let resolveConfirm; let…
3
votes
1 answer

React Typescript Argument of type 'x' is not assignable to parameter of type 'SetStateAction'

Here is the code trying to compile it says "Argument of type 'toDo' is not assignable to parameter of type 'SetStateAction'" i cannot figure out how it is not working since the type seem to be correct. How to avoid that error const…
Alberto3
  • 35
  • 2
  • 4
3
votes
2 answers

await function returns a promise and returns true even if the condition is false | React Functional Component

In my JSX file, I have a function like this. async isPresent () { const res = await AsyncFunction(); if (res) { return true; } return false; } and then using it in a conditional rendering as { this.isPresent() &&
3
votes
2 answers

Async function passed as prop into React component causing @typescript-eslint/no-misused-promises error

I have the following asynchronous submitNewPatient function which is throwing @typescript-eslint/no-misused-promises error message from elint. Is it possible to adjust the function such that it removes this error? const submitNewPatient = async…
3
votes
1 answer

React memo allowing re-rendering to forwardRef

I have a component defined as export default function MyComp({ ...someprops }) { const [data, setData] = useState([]); const searchRef = useRef(); return ( {!showEmptyState ? (
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
3
votes
0 answers

when i filter the cart the reducer function return proxy only in redux toolkit

I have tried to remove the product from the cart. here the cart is an array that contains products in the object. when I try to filter the cart in reducer it returns proxy and cannot do filtering while in same thing doing in normal redux do…
3
votes
1 answer

Should a function always be used for a setState that gets passed an array or object?

Wanting to improve my understanding of React in functional components I've seen some that pass a seState with a function when spreading an array or object. When I reference the docs I see: Unlike the setState method found in class components,…
DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127