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

How to access the DOM element of a third party component that doesn't forward ref

I am working with a third-party component which doesn't forward the ref to its DOM component and unfortunately, I need to get a ref to its DOM element in my code. The code below obviously fails: const ThirdPartyComponent = (props) => { return…
7
votes
2 answers

How can set Formik isSubmitting outside of my component when testing?

I have stateful functional component with a Formik form that renders different content based on the value of isSubmitting. const MyPage: FunctionComponent = ({propOne, propTwo}) => { ... {isSubmitting ? (
show…
user3096803
7
votes
3 answers

Is there a difference between storing a const value in a variable vs in state?

I've noticed a couple of ways of achieving seemingly the same thing inside a React functional component. When you have what is essentially a config value that is only needed inside this component (Just a constant value, never passed in or modified)…
DBS
  • 9,110
  • 4
  • 35
  • 53
6
votes
1 answer

Passing Tailwind class as a prop in React

I am creating a reusable component button to which I want to pass two Tailwind classes as props and use them dynamically. Here is my component: function Button({ primary, secondry, label, onClick }) { return (
6
votes
1 answer

What's the difference between functions that render JSX vs. declaring components inside another component?

Is this an anti-pattern? export function Todo() { ... const renderItem = (item) => ( item.done ? {item.text} : {item.text} ); return (
wrahim
  • 1,158
  • 4
  • 16
  • 33
6
votes
0 answers

how to document react functional components using Jsdocs, and Typescript

Given this type LocationWeather = { name: string; temperature: number; }; type IndexProp = { savedLocationsWeather: LocationWeather[]; favoriteLocationWeather: LocationWeather; }; function Index({ savedLocationsWeather,…
6
votes
2 answers

React App with both Functional and Class components

I know that after the emergence of React Hooks, functional components are able to act almost the same as the class components, and I have seen lately an encouragement wave to use functional components. My question is, could it hurt in any means to…
Ahmed Hammad
  • 2,798
  • 4
  • 18
  • 35
6
votes
2 answers

How to get size of an element in an react app that is totally based on functional components?

I have a react app that is purely built with functional components. I want to get the height of an element after it has been rendered on screen to change size of it's parent accordingly. There are solutions for class based apps but I couldn't find…
5
votes
3 answers

How does one trigger an action in a child functional component in React?

With a basic form/input layout, it's clear that a callback should be used for state changes from child to parent (initiated by child), but how can the parent ask the child component to re-assess its state and communicate that back to parent? The end…
5
votes
4 answers

Check if screen is getting blurred or focus in React native?

i am using this useEffect(() => { const navFocusListener = navigation.addListener('didFocus', () => { console.log('focus'); }); return () => { navFocusListener.remove(); }; }, []); I am using this code also…
Rover
  • 661
  • 2
  • 18
  • 39
5
votes
2 answers

How to Return a Page by ID with react-routing?

I'm just new to React and I'm having a hard time figuring out how to return a new page (a component) as I clicked the View button. I have a user table and per row has a View button. I can only retrieve the data in exactly the same page but I would…
5
votes
5 answers

Call api before first render in functional component in React.js

If I want to call API after the first rendering of component, I know we have useEffect hook to call the API method. (I am talking about functional components only. No class component). Is there any way, I can call the API before my component renders…
5
votes
1 answer

How to change context value in functional component?

I have a context named StatusContext like this: export const statusCtxInit = { open: false, toggleOpen() { this.open = !this.open; } }; const StatusContext = React.createContext(statusCtxInit); export default StatusContext The…
Behnam Azimi
  • 2,260
  • 3
  • 34
  • 52
4
votes
2 answers

React: Event handlers inside or outside functional component?

Consider this simple TextField component in React: import React, { useState } from "react"; import { TextField, Grid } from "@mui/material"; const handleEnter = (event) => { console.log("In handleEnter"); if (event.key == "Enter" &&…
4
votes
0 answers

Should I use cyclomatic complexity in React?

I'm working on a React application using functional components. The lint configuration file has Cyclomatic Complexity enabled. That means that every component or custom hook that has too many logical conditions is marked with an error. Cyclomatic…
1
2
3
86 87