我的應用程式有一個測驗表,如果用戶通過測驗,他會顯示一個通過螢屏,然后使用 asyncstorage 保存狀態。但這里的問題是,假設我有用戶 A 和用戶 B,并且用戶 A 當前已登錄,他通過了測驗,應用程式顯示他通過螢屏并保存狀態。現在用戶 A 注銷并且用戶 B 登錄,他是一個全新的用戶,他以前從未進行過測驗,但我的應用程式仍然保存了用戶 A 的狀態,并且甚至向用戶 B 顯示通過螢屏,而不是它不應該。有人可以幫我解決這個問題嗎?
代碼:
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 天里一直在處理它!
uj5u.com熱心網友回復:
我認為這是問題所在:
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))
}
uj5u.com熱心網友回復:
我不知道您的代碼到底出了什么問題,但我相信無論誰登錄(狀態持久性),您的 useEffect 中的這段代碼都會獲取用戶 A 的狀態。嘗試使用用戶 C 進行測驗。在他們的官方檔案中查看 firebase 狀態持久性。我希望我給了你一些提示來解決這個問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/475094.html
標籤:javascript 反应式 使用效果 使用状态 异步存储
