React: 'setState' error while use 'useEffect'

Created on 23 Dec 2018  路  2Comments  路  Source: facebook/react

Do you want to request a feature or report a bug?
bug
What is the current behavior?
issue_bug_1
issue_bug_2

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:

What is the expected behavior?
I want to update state when I componentdidmount, but it can lead to errors, and I don't know what to do.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
issue_bug_3

Most helpful comment

Your current code is calling setState() _every_ time the component re-renders. Since setState() queues a re-render, you're creating an infinite update loop.

In order to have this happen _only_ the first time the component renders, you need to pass an empty array as the second argument to useEffect().

I'd encourage you to read the Hooks docs thoroughly to better understand how to use them correctly:

https://reactjs.org/docs/hooks-intro.html

All 2 comments

Your current code is calling setState() _every_ time the component re-renders. Since setState() queues a re-render, you're creating an infinite update loop.

In order to have this happen _only_ the first time the component renders, you need to pass an empty array as the second argument to useEffect().

I'd encourage you to read the Hooks docs thoroughly to better understand how to use them correctly:

https://reactjs.org/docs/hooks-intro.html

Your current code is calling setState() _every_ time the component re-renders. Since setState() queues a re-render, you're creating an infinite update loop.

In order to have this happen _only_ the first time the component renders, you need to pass an empty array as the second argument to useEffect().

I'd encourage you to read the Hooks docs thoroughly to better understand how to use them correctly:

https://reactjs.org/docs/hooks-intro.html
Thks, it has been solved, i think i have to read the docs again....馃槉

Was this page helpful?
0 / 5 - 0 ratings

Related issues

trusktr picture trusktr  路  3Comments

jimfb picture jimfb  路  3Comments

zpao picture zpao  路  3Comments

jvorcak picture jvorcak  路  3Comments

hnordt picture hnordt  路  3Comments