首页 > 解决方案 > 为什么状态没有正确更新?

问题描述

我尝试调用服务器,然后运行地图循环。显然状态在运行期间没有充分更新,我应该做些什么不同的事情才能让它正确运行?

export default function User() {
    const classes = useStyle();
    const [users, setUsers] = useState();

    useEffect(() => {
        const GetAllUsers = async () => {
            let resp = await axios.get("http://localhost:8000/users");
            resp = await resp.data;
            console.log(resp);
            setUsers(resp);
            console.log(users);
        };
        GetAllUsers();
    }, [users]);

    const edit = () => {};

    return (
        <Card className={classes.root}>
            <CardContent>
                <Typography variant="h5" component="h2"></Typography>
                <Typography variant="h9" component="p">
                    {users.map((user, index) => {
                        <Typography key={index}>
                            Name : {user.name} <br></br>, User Name :{" "}
                            {user.UserName} <br></br>, Session time out
                            (Minutes) : <br></br>, Created data : <br></br>,
                            Premissions :,
                        </Typography>;
                    })}
                </Typography>
            </CardContent>
            <CardActions>
                <Button className={classes.btn} size="small" onClick={edit}>
                    Edit
                </Button>
                <Button className={classes.btn} size="small">
                    Delete
                </Button>
            </CardActions>
        </Card>
    );
}

标签: react-hooks

解决方案


推荐阅读