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


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?

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:
Your current code is calling
setState()_every_ time the component re-renders. SincesetState()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....馃槉
Most helpful comment
Your current code is calling
setState()_every_ time the component re-renders. SincesetState()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