我有一个问题,我不明白为什么钩子返回 undefined :
import React, { useEffect, useState } from 'react';
function App(){
const [globalVariable, setGlobalVariable] = useState();
useEffect(()=>{
const test = 5
console.log(test) // return 5
setGlobalVariable(test)
console.log(globalVariable) // return undefined
},[]);
return (
<div>
</div>
);
}
export default App;
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能直接为 globalVariable 设置新值?