DEV Community

loading...

Discussion on: React: useEffect

Collapse
pony profile image
Pony

Oh, Right
Re-render issue, I think
I want to know what is caused by and how to solve this

Thanks in advance.

Thread Thread
koralarts profile image
Karl Castillo Author • Edited

Let's say you have a useState which returns a setter that we named setValue. If we call setValue outside of any function in our component, it would cause a re-render issue.

const App = () => {
  const [value, setValue] = useState('')
  setValue('value')
  return <p>{value}</p>
}

Another way that would cause this is if two useEffect depend on a value that the other updates.

const [value1, setValue1] = useState('')
const [value2, setValue2] = useState('')
useEffect(() => {
  setValue1('value1')
}, [value2])

useEffect(() => {
  setValue2('value2')
}, [value1])

For more information, you can look at Rules of Hooks

Thread Thread
pony profile image
Pony

Thanks,
It really helps me a great deal.
Appreciate you

Thread Thread
pony profile image
Pony

Thanks, Karl
This really helps me a great deal.
Appreciate your help