React Native-调用来自另一个组件的异步函数存储并获取数据

发布于 2025-01-31 08:36:51 字数 1129 浏览 1 评论 0原文

我正在尝试调用异步函数存储并获取我从API获取的数据。用户应该在登录屏幕上获得令牌,并且令牌将显示在主屏幕上。但是当用户按主屏幕上的按钮时,控制台不会显示令牌 link.js

export const SET_USER_AUTH = async ({accessToken, tokenType, expiresAt}) => {
    try {
        await AsyncStorage.setItem('@access_token', accessToken);
        await AsyncStorage.setItem('@token_type', tokenType);
        await AsyncStorage.setItem('@expires_at', expiresAt);
        await AsyncStorage.setItem('@user_auth', tokenType + " " + accessToken);
        console.log("Done SET_USER_AUTH");
    } catch(e) {
    // read error
    }
};

export const GET_USER_AUTH = async () => {
    try {
        const accessToken = await AsyncStorage.getItem('@access_token');
        const tokenType = await AsyncStorage.getItem('@token_type');
        const expiresAt = await AsyncStorage.getItem('@expires_at');
        const userVer = await AsyncStorage.getItem('@user_auth');
        console.log("Done GET_USER_AUTH");
    } catch(e) {
    // read error
    }
};
    

home.js

return (
        <Button onPress={() => console.log(GET_USER_AUTH.userVer)}/>
    );

I'm trying to call an async function to store and get data that I fetch from an API. The user is supposed to get a token on the login screen, and the token will be shown on the home screen. But the token is not shown in the console when the user presses the button on the home screen
link.js

export const SET_USER_AUTH = async ({accessToken, tokenType, expiresAt}) => {
    try {
        await AsyncStorage.setItem('@access_token', accessToken);
        await AsyncStorage.setItem('@token_type', tokenType);
        await AsyncStorage.setItem('@expires_at', expiresAt);
        await AsyncStorage.setItem('@user_auth', tokenType + " " + accessToken);
        console.log("Done SET_USER_AUTH");
    } catch(e) {
    // read error
    }
};

export const GET_USER_AUTH = async () => {
    try {
        const accessToken = await AsyncStorage.getItem('@access_token');
        const tokenType = await AsyncStorage.getItem('@token_type');
        const expiresAt = await AsyncStorage.getItem('@expires_at');
        const userVer = await AsyncStorage.getItem('@user_auth');
        console.log("Done GET_USER_AUTH");
    } catch(e) {
    // read error
    }
};
    

Home.js

return (
        <Button onPress={() => console.log(GET_USER_AUTH.userVer)}/>
    );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文