首页 > 解决方案 > 为什么我的 setstate 给我未定义?反应

问题描述

我有这个 useState 声明:

const [userLogged, setUserLogged] = useState();

这是我在自定义钩子中的代码片段:

await axios.post('http://localhost:3000/users/register', form).then( (res)=> {
  setUserLogged(res.config.data)
  console.log(UserLogged)
  window.location.href = "http://localhost:3001/"
});

当我的回调工作时,我想从 res设置userLogged使用用户对象的状态,但它不起作用。如果一个状态,我得到一个。setUserLoggedconsole.loguserLoggedundefined

我尝试通过将布尔值或字符串传递给setUserLogged进行测试,但它也未定义。

标签: javascriptreactjscallbacksetstateuse-state

解决方案


推荐阅读