在 React Native 中没有为新用户更新状态

state is not getting updated for a new user in react native

我的应用程序有一个测试 sheet,如果用户通过测试,他会看到一个通过的屏幕,然后使用异步存储保存状态。但这里的问题是,假设我有用户 A 和用户 B,用户 A 当前已登录,他通过了测试,应用程序显示他通过了屏幕,状态已保存。现在用户 A 注销,用户 B 登录,他是一个全新的用户,他以前从未进行过测试,但我的应用程序仍然保存了用户 A 的状态,并不断向用户 B 显示通过屏幕,而不是它应该 not.Can 有人帮我解决这个问题吗?

代码:

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 天一直在处理它!

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

我认为这是问题所在:

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 的值不会(我认为)。不要将密钥存储为字符串,而是尝试将其存储为函数:

// 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))
}