感谢您花时间阅读我的帖子!
我想做的事: 我想保存我的运动计数。知道如何使用异步设置/获取/删除另一个值也会很有趣。
发生了什么: 当我按下按钮保存它 (setItem)或当我在 useEffect 中调用 save() 时,它会使我的应用程序崩溃,另一方面,getItem 和 removeItem 工作正常!
我所做的: 我确实尝试在保存函数中使用 JSON.stringify(exerciseCount) 并且它只工作了一次,而且如果我再次按下它,它会继续覆盖在“”中。
我注释掉的笔记只是我试图获得另一个价值,但我不确定它如何用于异步,所以如果有人也有一些提示,那就太好了!
我的代码:
import React, { useContext, useState, useEffect } from "react";
import {
StyleSheet,
Text,
View,
Image,
AsyncStorage,
TouchableOpacity,
} from "react-native";
import { ExerciseContext } from "../components/ExerciseContext";
import { AntDesign } from "@expo/vector-icons";
import { Colors } from "../colors/Colors";
//import { TouchableOpacity } from "react-native-gesture-handler";
export default function StatsScreen() {
const [exerciseCount, setExerciseCount] = useContext(ExerciseContext);
//const [workoutCount, setWorkoutCount] = useContext(ExerciseContext);
const save = async () => {
try {
await AsyncStorage.setItem("MyExerciseCount", exerciseCount );
//await AsyncStorage.setItem("MyWorkoutCount", workoutCount);
} catch (err) {
alert(err);
}
};
const load = async () => {
try {
let exerciseCount = await AsyncStorage.getItem("MyExerciseCount");
//let workoutCount = await AsyncStorage.getItem("MyWorkoutCount");
if (exerciseCount !== null) {
setExerciseCount(exerciseCount);
}
// if (workoutCount !== null) {
// setWorkoutCount(workoutCount);
// }
} catch (err) {
alert(err);
}
};
const remove = async () => {
try {
await AsyncStorage.removeItem("MyExerciseCount");
} catch (err) {
alert(err);
} finally {
setExerciseCount(0);
}
};
useEffect(() => {
load();
//save();
}, []);
return (
<View style={styles.container}>
<Image
source={require("../../assets/mindfulness.png")}
style={{ width: "100%", height: 200, marginTop: 14 }}
resizeMode="contain"
/>
<View style={styles.cardWide}>
<AntDesign
style={styles.iconOne}
name="Trophy"
size={24}
color="white"
/>
<Text style={styles.headerOne}>Exercises Completed:</Text>
<Text style={styles.exerciseNumber}> {exerciseCount}</Text>
<TouchableOpacity style={styles.save} onPress={() => save()}>
<Text style={{ color: "white", fontWeight: "bold" }}>Save</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.remove} onPress={() => remove()}>
<Text style={{ color: "white", fontWeight: "bold" }}>Restart</Text>
</TouchableOpacity>
</View>
<View style={styles.cardWide}>
<AntDesign
style={styles.iconOne}
name="staro"
size={24}
color="white"
/>
<Text style={styles.headerOne}>Workouts Completed:</Text>
<Text style={styles.exerciseNumber}> {exerciseCount}</Text>
<TouchableOpacity style={styles.remove} onPress={() => remove()}>
<Text style={{ color: "white", fontWeight: "bold" }}>Restart</Text>
</TouchableOpacity>
</View>
{/* <TouchableOpacity onPress={() => save()}>
<Text>Save</Text>
</TouchableOpacity> */}
</View>
);
}
const styles = StyleSheet.create({})
如果您对我的锻炼上下文如下所示感到好奇:
import React, { useState, createContext } from "react";
const ExerciseContext = createContext([{}, () => {}]);
const ExerciseProvider = (props) => {
const [state, setState] = useState(0);
//{ exerciseCount: 0, workoutCount: 0 }
return (
<ExerciseContext.Provider value={[state, setState]}>
{props.children}
</ExerciseContext.Provider>
);
};
export { ExerciseContext, ExerciseProvider };
再次感谢任何可以提供帮助的人!杰克。