我试图在注销时清除 AsynStorage

问题描述

我正在使用 React Native 开发一个应用程序,并且我在使用 AsyncStorage 时遇到了这个问题。我在登录时存储用户数据,然后在主页上获取数据。我只是想在一条小的欢迎消息中显示用户名,然后如果用户退出,我需要清除存储空间,然后在设备上获取一个登录用户的存储空间并显示他们的名字。

问题是我第一次登录不显示用户名,只有当我退出并重新登录时才显示名称。不仅如此,它似乎还会保留上次登录的人的姓名,并在下次登录显示该姓名。

任何帮助将不胜感激。

这是我获取显示用户名的主要代码


    AsyncStorage.getItem('user_data',(err,result) => {
        console.log(result);
        var userdata = JSON.parse(result);
        console.log(userdata);
        firstname = userdata.firstName;
        lastname = userdata.lastName;
    });

    const history = useHistory();
    const dologout = event => {
        event.preventDefault();
        history.push('/');
        AsyncStorage.removeItem('user_data',result) => {
            console.log(result);
        });
    };

    return (
        <View style={styles.container}>
            <Text>Welcome {firstname} </Text>
            <View style={{ marginTop: 10 }}>
                <TouchableOpacity
                    style={styles.loginbut}
                    activeOpacity={.5}
                    onPress={dologout}
                >
                    <View style={{ flexDirection: 'row',alignSelf: 'center' }}>
                        <FontAwesomeIcon icon={faSignOutAlt} size={20} style={{ color: '#ffffff' }} />
                        <Text style={{ textAlign: 'center',fontWeight: 'bold',color: 'white' }}> Sign Out </Text>
                    </View>
                </TouchableOpacity>
            </View>
        </View>
     );

} export default MainUI```

解决方法

试试这个方法

async removeItemValue(key) {
    try {
        await AsyncStorage.removeItem(key);
        return true;
    }
    catch(exception) {
        return false;
    }
}
,

如果您想在注销后清除它,我认为最好使用df$Descriptions = str_replace_all(df$Descriptions,"[^[:graph:]]"," ") redux 在进程运行期间存储用户数据。

context 类似于您想在用户退出进程后存储一些数据。

喜欢记录电子邮件或一些应用程序设置。