使用useState设置值后,使用时没有拿到最新数据解决办法

使用时在useEffect中注入对应依赖

    const [address, setAddress] = useState<string>(userAddress??"");
    useEffect(() => {
        Async(async () => {
            const request = new GetUserStatByDIDRoleRequest();
            request.address =  address;
            const [userInfo, err] = await GetUserStatByDIDRole(request);
            if (err) {
                message.error(`Error: GetUserStatByDIDRoleUserPage is fail: ${err}`)
                return;
            }
            userInfo.address = address;
            await loadDIDAllRoleStat(address);
            setUserInfo(userInfo);
        })
    }, [address]);

你可能感兴趣的:(javascript,前端,开发语言)