首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >状态未为新用户进行更新,以响应本机。

状态未为新用户进行更新,以响应本机。
EN

Stack Overflow用户
提问于 2022-05-14 06:59:27
回答 2查看 54关注 0票数 0

我的应用程序有一个测试表,如果用户通过测试,他会被显示为一个通过测试的屏幕,然后使用异步存储保存状态。但问题是,假设我有用户A和用户B,用户A目前正在登录,他通过了测试,应用程序显示他通过屏幕,状态保存。现在,用户A登录,用户B登录,他是一个全新的用户,他以前从未进行过测试,但我的应用程序仍然保存了用户A的状态,并且一直在显示通过屏幕的用户B,而应该有人帮助我解决这个问题吗?

代码:

代码语言:javascript
复制
import React ,{useState, useEffect} from "react";
import {View, Alert, Image, StyleSheet, Text, Modal, TouchableOpacity, TouchableHighlight} from 'react-native';
import Voice from 'react-native-voice';
import auth from '@react-native-firebase/auth';


import AsyncStorage from '@react-native-async-storage/async-storage';
  const key = auth().currentUser.uid + "hasPassed"
  

export const hasPassed = async () => {
    return AsyncStorage.getItem(key).then(result => result != null ? JSON.parse(result) : undefined).catch(e => console.log(e))    
}

export const setHasPassed = async (newPassed) => {
    return AsyncStorage.setItem(key, JSON.stringify({hasPassed: newPassed})).catch(e => console.log(e))
}



export default alpht =({navigation}) => { 

 
 function Check() {
  if (results.includes(words[index])){
 
    Alert.alert('Correct!','You are learning so well!');
    
     if(index==7) {
      if(count<=5)
      {
           
        setHasPassed(true).then(() => setshowpass(true))
        //  setshowpass(true);
      }
      else{
        console.log(count)
        Alert.alert('fail','fail');
      }
    }
    if (index==7){
      setndis(true);
      setdis(true);
      setidis(true);
    }
    else{
   setndis(false);
   setdis(true);
   setidis(true);
    }
   
  }
  else{
    Alert.alert('Ops!','Looks like you went wrong somewhere. Try again!');
    setcount(count+1);
    
    setdis(true);
    setndis(true);
   
    if(count==5){
      Alert.alert('Restest', 'Looks like you had way too many mistakes!')
      setind(0);
      setcount(0);
      setdis(true);
    }
  }
}


  const words=['ceket', 'çilek', 'elma', 'fare', 'öğretmen', 'otobüs', 'şemsiye', 'uçak'];
  const [show, setshow]=useState('');
    const [showpass, setshowpass]=useState(false);
    useEffect(() => {
     //console.log(auth().currentUser.uid);
        setshow(true);
      }, []);

      useEffect(() => {
        const getState = async () => {
          const result = await hasPassed()
          setshowpass(result ? result.hasPassed : false)
      }
      getState()
     
      }, []);

     
      console.log(auth().currentUser.uid)
      if (showpass === false) {

         
        // setshow(true)
        console.log('hey');
       return null
      }
return (
//... other code
)
}

顺便说一下,我的用户使用auth().signOut()登录!如果这个问题解决了,那就太好了,我在过去的4,5天里一直在处理它!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-05-14 09:59:44

我想这就是问题所在:

代码语言:javascript
复制
const key = auth().currentUser.uid + "hasPassed"
  
export const hasPassed = async () => {
    return AsyncStorage.getItem(key).then(result => result != null ? JSON.parse(result) : undefined).catch(e => console.log(e))    
}

export const setHasPassed = async (newPassed) => {
    return AsyncStorage.setItem(key, JSON.stringify({hasPassed: newPassed})).catch(e => console.log(e))
}

key是在反应生命周期之外的顶层定义的,因此它受制于陈旧的值。auth().currentUser可能会改变,key的值不会改变(我认为)。与其将键存储为字符串,不如将其存储为函数:

代码语言:javascript
复制
// every time getKey is called it will get a fresh instance of currentUser
const getKey = ()=>auth().currentUser.uid + "hasPassed"

export const hasPassed = async () => {
  return AsyncStorage.getItem(getKey()).
    then(result => result != null ? JSON.parse(result) : undefined).
    catch(e => console.log(e))    
}

export const setHasPassed = async (newPassed) => {
  return AsyncStorage.setItem(
    getKey(),
    JSON.stringify({hasPassed: newPassed})
  ).catch(e => console.log(e))
}
票数 1
EN

Stack Overflow用户

发布于 2022-05-14 07:42:59

我不知道您的代码到底出了什么问题,但我相信您的useEffect中的代码段正在获取用户A的状态,不管是谁登录(状态持久性)。尝试使用用户C进行测试。请在他们的官方文档中查看火基状态持久性。我希望我能给你一些解决这个问题的提示。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72238018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档