我有这个代码,我的问题是为什么在我的答案函数中我得到了初始状态。如何以正确的方式设置状态以在 setTimeout 函数的回调中获得正确的状态?
const App = () => {
const [state, setState] = useState({
name: "",
password: "",
});
useEffect(() => {
setState({ ...state, password: "hello" });
setTimeout(answer, 1000);
}, []);
const answer = () => {
console.log(state);
// we get initial State
};
return <div className="App"></div>;
};