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
1 answer

Updating a specific field of object state in React

I am using a React functional component where I want to update only a specific field of the state and retain the other values like before. Here's the state initialization - const[value, setValue] = React.useState({ a: "5", b: "6" }); I…
4
votes
2 answers

What is the recommended approach for large react app with react-query?

I've recently started using react using functional components and react-query and it has been working fine except that I'm not clear about how to organize components properly. the way I'm used to designing components is by having a top level…
4
votes
2 answers

how to stop multiple re-renders from doing multiple api calls useEffect?

I'm new to react functional components and I'm trying to get the weather data on multiple cities on page load but useEffect is now re-rending each call. How can I write this so useEffect doesn't cause re-renders? function App() { const [data,…
4
votes
1 answer

How can I pass props from a functional parent component that uses the state hook?

So I am working on this UserData App (for practicing purposes). I have one component where I store hardcoded user data - an array with different user objects. In my app, I want a list of users (which displays usernames only) on the left side of the…
4
votes
0 answers

React Functional Component with useState Hook Breaks Component Library

I'm creating a library of custom react components and I'm running into an issue that I think must be related to how my package is getting created. When I add a functional component that utilizes the 'useState' hook to my library and import it as a…
4
votes
2 answers

React Native setState not working (functional component) at all, not even just out of sync

I appreciate anyone taking the time to go through my code and helping me with what's going wrong (printLang() is always printing 'en' or 'es' depending on which value it took on when I refreshed the app)! I'm using Material Dropdown for the first…
4
votes
5 answers

TypeError: Cannot read property 'getPosts' of undefined - useQuery hook, react Functional Components

I did try searching for the same question but all of those were of either angular or unrelated, I am trying to make a Social app using MongoDB, Express, React, Node, Graphql with Apollo, I am following a video from freecodecamp : Link to the…
4
votes
1 answer

How to pass function as props from one functional component to another?

I'm passing a function as prop from one functional component(Dashboard) to another (Event).However, When I'm clicking the button on the child component which holds the passed function prop on the onClick value , it throws an error. Uncaught…
4
votes
1 answer

How can I update multiple states at a time with useState hook?

In functional components, we use useState as opposed to this.setState for classComponents. But as far as I know, you can only set one state at a time with useState, while setState lets you set multiple states at a time, (e.g. this.setState({a1:…
4
votes
3 answers

Typescript React Functional Component with props.children

I'm trying to make a component that will show a loading circle when the prop isLoading is true, and otherwise show the child component. I'd like to use the component in other components likes this... import Loading from './Loading.tsx' ... const…
M. Stolte
  • 105
  • 1
  • 1
  • 11
4
votes
1 answer

'Property does not exist' in a Functional Component with added functional components as properties to it?

I use React with Typescript and a functional approach. const Divider: React.FunctionComponent = (props: CardDividerProps) => (
divider
); const Card: React.FunctionComponent = (props: CardProps)…
4
votes
3 answers

Are parameters needed for inner functions to access props + variables of a React Functional Components

I have a functional component that has a couple of functions inside of it. In the example below, the inner functions are function1 and function2. I am trying to figure out if i need to explicitly pass the functional component's props (prop1, prop2),…
Canovice
  • 9,012
  • 22
  • 93
  • 211
4
votes
2 answers

In React, can I define a functional component within the body of another functional component?

I've started seeing some of my team writing the following code which made me question if we're doing things the right way as I hadn't seen it written like this before. import * as React from "react"; import "./styles.css"; function UsualExample()…
Justyn
  • 1,442
  • 12
  • 27
4
votes
1 answer

React - Typescript interfaces - errors when get props from union types

I use react with typescript. I want to require only one parameter be accepted from prop3 | prop4. I use a union type like this: interface General { prop1?: boolean prop2?: boolean } interface Option1 extends General { prop3:…
ESI
  • 1,657
  • 8
  • 18
4
votes
2 answers

Array of Refs in functional component to change classnames of individual items via classList

I'm converting my jQuery based project to react and run into an issue with refs in a functional component: I have a huge wordlist and each word should be accessed via their own ref so that I can change individual classNames, depending on what the…
Christian Strang
  • 8,470
  • 5
  • 42
  • 53
1 2
3
86 87