-
Can you use useState variable_1 as default value for another useState variable_2, especially when variable_1 is being set by useEffect?
-
Even simpler, can you use useState variable_1 as default value for another useState variable_2 (without the useEffect dependency)?
To elaborate, here’s an example
const [firstName, setFirstName] = useState("DEFAULT_NAME");
const [fullName, setFullName] = useState(firstName); //is doing this permissible?
useEffect(() => {
setFirstName("REAL_NAME")
}, []);
console.log(fullName); // what would be printed here?
What would the value of fullName
be? Would it be set to "DEFAULT_NAME"
(signaling that it was set BEFORE the useEffect was called) or be set to "REAL_NAME"
(signaling that it was set AFTER the useEffect was called)?