I am new to React and have a hard time understanding why when I call useEffect after I just set the new state on a variable, the new value is undefined. Can someone also explain why the second UseEffect hook is called twice. Is it because it’s called on initial mount, and then called when it finally changes state from the first useEffect? How can I get it to fire only once val has a defined value that was set in the first useEffect?
import React from 'react';
import {useState, useEffect} from 'react'
export function App(props) {
const [val, setVal] = useState();
useEffect(() => {
setVal("hello");
},[]);
useEffect(() => {
console.log("value: ", val);
}, [val])
return (
<div className='App'>
</div>
);
}
2
Answers
You do not need more than one
useEffect
. In this instance one would work:the
useEffect
you’re showing can change whenever thestate
is changed an example with updating state from anonClick
:The code is working exactly as I’d expect it to.
On (or more specifically, immediately after) the first render, both effects execute. Which:
undefined
to the consoleThen when state is updated, the component re-renders. This re-render has a new state value for
val
, which triggers the seconduseEffect
to execute. Which:"hello"
to the consoleIf you want something to happen conditionally, you’d use an
if
statement. For example:This will only log
val
to the console if it has a "truthy" value.