希望应用程序根据react native中的每个用户分别保存状态



我有一个进行测试的应用程序,当当前登录的用户通过测试时,应用程序显示一个模态,告诉他他已经通过了测试。我希望应用程序为当前登录的特定用户保存此状态,即使他关闭应用程序,通过的屏幕仍然保留在那里,但现在如果一个全新的用户登录,应用程序应该从头开始,它应该首先显示测试屏幕,然后如果用户通过测试,应用程序相应地为他保存状态。

长话短说,我希望我的应用程序根据每个用户的情况分别保存状态(而不是他们是否通过了测试)。我想要一个简单明了的解决办法。下面是test.js的代码片段:

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';
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)
{

//displaying the modal for passing screen
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(() => {
setshow(true);
}, []);
return (
..other code here
)
}

有多种方法可以解决这个问题。您可以使用AsyncStorage或使用react-redux和redux-persist构建更复杂的逻辑。

底线,你只需要缓存用户详细信息/数据,你可以按照我上面的建议去做。(我的答案不需要代码片段)

谢谢。

最新更新