'Reactnative asyncstorage for a specific user is not working properly
I am trying to save the passing screen after a user that is logged in, has passed the test. And that passing screen is saved even after the app is completely closed. For this purpose, I am using asyncstorage but the problem is that if once for a user, a passing screen is saved, even if a new user logs in, he does not even get the test sheet, the app directly lands on the passing screen even for a completely new user. I will be grateful if someone helps, i am stuck here for past 3 days. Here is the code:
For test.js:
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 test =({navigation}) => {
const [index, setind] = useState(0);
const [idis, setidis] = useState(false);
const [count, setcount] = useState(0);
const [error, setError] = useState('');
const [results, setResults] = useState('');
const [disb, setdis] = useState(true);
const [ndisb, setndis] = useState(true);
useEffect(() => {
//Setting callbacks for the process status
Voice.onSpeechStart = onSpeechStart;
Voice.onSpeechEnd = onSpeechEnd;
Voice.onSpeechResults = onSpeechResults;
Voice.onSpeechError = onSpeechError;
return () => {
//destroy the process after switching the screen
Voice.destroy().then(Voice.removeAllListeners);
};
}, []);
check function for test.js:
function Check() {
if (results.includes(words[index])){
// console.log(index);
Alert.alert('Correct!','You are learning so well!');
if(index==7) {
if(count<5)
{
setHasPassed(true).then(() => setshowpass(true))
}
else{
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);
console.log('uc',count);
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=['word1', 'wor2', 'word3', 'word4', 'wrd5', 'word6', 'wor7', 'w8'];
const [showpass, setshowpass]=useState(false);
useEffect(() => {
const getState = async () => {
const result = await hasPassed()
setshowpass(result ? result.hasPassed : false)
}
getState()
}, []);
if (showpass === undefined) {
return null
}
return (
<View style={styles.body}
>
<Modal
transparent={false}
visible={showpass}
animationType='slide'
hardwareAccelerated
>
<View style={styles.body}>
<Text style={styles.toptext}>Congratulations!</Text>
<Text style={styles.toptext}>Worked great in Turkishya!</Text>
<Text style={styles.topptext}>
And mastered the skill 'Alphabets'
</Text>
</View>
</Modal>
<View>
);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|