Skip to main content

Questions tagged [react-hooks]

React Hooks are a feature that allows developers to use state(s) and other React component lifecycle features without writing a class-based component.

862 votes
22 answers
855k views

The useState set method is not reflecting a change immediately

I am trying to learn hooks and the useState method has made me confused. I am assigning an initial value to a state in the form of an array. The set method in useState is not working for me, both with ...
Pranjal's user avatar
  • 8,733
857 votes
23 answers
1.2m views

How to fix missing dependency warning when using useEffect React Hook

With React 16.8.6 (it was good on previous version 16.8.3), I get this error when I attempt to prevent an infinite loop on a fetch request: ./src/components/BusinessesList.js Line 51: React Hook ...
russ's user avatar
  • 8,613
551 votes
20 answers
811k views

React Hook Warnings for async function in useEffect: useEffect function must return a cleanup function or nothing

I was trying the useEffect example something like below: useEffect(async () => { try { const response = await fetch(`https://www.reddit.com/r/${subreddit}.json`); const json = ...
RedPandaz's user avatar
  • 5,966
718 votes
24 answers
497k views

React Hooks: useEffect() is called twice even if an empty array is used as an argument

I am writing code so that before the data is loaded from DB, it will show loading message, and then after it is loaded, render components with the loaded data. To do this, I am using both useState ...
J.Ko's user avatar
  • 8,141
276 votes
15 answers
156k views

State not updating when using React state hook within setInterval

I'm trying out the new React Hooks and have a Clock component with a time value which is supposed to increase every second. However, the value does not increase beyond one. function Clock() { ...
Yangshun Tay's user avatar
  • 52.3k
203 votes
18 answers
234k views

Infinite loop in useEffect

I've been playing around with the new hook system in React 16.7-alpha and get stuck in an infinite loop in useEffect when the state I'm handling is an object or array. First, I use useState and ...
WhiteFluffy's user avatar
  • 4,775
358 votes
20 answers
339k views

Make React useEffect hook not run on initial render

According to the docs: componentDidUpdate() is invoked immediately after updating occurs. This method is not called for the initial render. We can use the new useEffect() hook to simulate ...
Yangshun Tay's user avatar
  • 52.3k
62 votes
5 answers
26k views

Why is React useState with functional update form is needed?

I'm reading React Hook documentation about functional updates and see this quote: The ”+” and ”-” buttons use the functional form, because the updated value is based on the previous value But I can'...
likern's user avatar
  • 3,914
329 votes
56 answers
981k views

Invalid hook call. Hooks can only be called inside of the body of a function component

I want to show some records in a table using React but I got this error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the ...
lwin's user avatar
  • 4,280
302 votes
20 answers
382k views

How can I use multiple refs for an array of elements with hooks?

As far as I understood I can use refs for a single element like this: const { useRef, useState, useEffect } = React; const App = () => { const elRef = useRef(); const [elWidth, ...
devserkan's user avatar
  • 17.4k
412 votes
23 answers
408k views

How to use `setState` callback on react hooks

React hooks introduces useState for setting component state. But how can I use hooks to replace the callback like below code: setState( { name: "Michael" }, () => console.log(this.state) ); I ...
Joey Yi Zhao's user avatar
  • 41.1k
382 votes
9 answers
589k views

Push method in React Hooks (useState)?

How to push element inside useState array React hook? Is that as an old method in react state? Or something new? E.g. setState push example ?
Milos N.'s user avatar
  • 4,743
25 votes
2 answers
12k views

React useEffect in depth / use of useEffect?

I am trying to understand the useEffect hook in-depth. I would like to know when to use which method and why? useEffect with no second paraments useEffect(()=>{}) useEffect with second paraments ...
John_ny's user avatar
  • 787
118 votes
7 answers
60k views

Does React batch state update functions when using hooks?

For class components, this.setState calls batch if inside event handlers. But what happens if state is updated outside the event handler and using useState hook? function Component() { const [a, ...
vadirn's user avatar
  • 1,735
118 votes
16 answers
157k views

How can I use React hooks in React classic `class` component?

In this example, I have this react class: class MyDiv extends React.component constructor(){ this.state={sampleState:'hello world'} } render(){ return <div>{this.state....
Aminadav Glickshtein's user avatar

15 30 50 per page
1
2 3 4 5
110