【发布时间】:2021-04-20 15:36:06
【问题描述】:
export default function Main({ match }) {
const userid = match.params.id;
const [user, setUser] = useState([]);
async function fetchuser() {
const response = await api.get('/emps/profile', {
headers: {
userid: match.params.id,
},
});
setUser(response.data);
console.log(response.data);
console.log(user);
}
useEffect(() => {
fetchuser();
}, [match.params.id]);
在上面的代码中,response.data 被写入控制台,但用户状态为空。谁能告诉我这是为什么?
【问题讨论】:
-
这可能是由于
useState的异步特性。您的状态不会立即设置。
标签: reactjs express react-hooks react-state