Hello,
I’m a bit confused, and I have this React Native program. I would like that when the date changes and the app is restarted, resetBars(); is called in this function, and otherwise, the bars are loaded normally. this is my Use Effect:
// Hier rufen Sie die gespeicherten Balkenhöhen aus AsyncStorage beim Komponentenstart ab
useEffect(() => {
const getBalkenHeights = async () => {
try {
const currentDate = new Date().toDateString();
const storedDate = await AsyncStorage.getItem('storedDate');
if (storedDate === null || storedDate !== currentDate) {
await AsyncStorage.setItem('storedDate', currentDate);
console.log('anders');
} else {
const sodiumHeightString = await AsyncStorage.getItem('sodiumHeight');
// Überprüfen, ob die Balkenhöhe bereits in AsyncStorage gespeichert ist
if (sodiumHeightString !== null) {
setSodiumHeight(parseFloat(sodiumHeightString));
} else {
// Wenn nicht, setzen Sie den Startwert auf 0
setSodiumHeight(0);
}
// Wiederholen Sie diesen Vorgang für alle Balkenhöhen
const magnesiumHeightString = await AsyncStorage.getItem('magnesiumHeight');
if (magnesiumHeightString !== null) {
setMagnesiumHeight(parseFloat(magnesiumHeightString));
} else {
setMagnesiumHeight(0);
}
const calciumHeightString = await AsyncStorage.getItem('calciumHeight');
if (calciumHeightString !== null) {
setCalciumHeight(parseFloat(calciumHeightString));
} else {
setCalciumHeight(0);
}
const potassiumHeightString = await AsyncStorage.getItem('potassiumHeight');
if (potassiumHeightString !== null) {
setPotassiumHeight(parseFloat(potassiumHeightString));
} else {
setPotassiumHeight(0);
}
const lithiumHeightString = await AsyncStorage.getItem('lithiumHeight');
if (lithiumHeightString !== null) {
setLithiumHeight(parseFloat(lithiumHeightString));
} else {
setLithiumHeight(0);
}
const boronHeightString = await AsyncStorage.getItem('boronHeight');
if (boronHeightString !== null) {
setBoronHeight(parseFloat(boronHeightString));
} else {
setBoronHeight(0);
}
const carbohydrateHeightString = await AsyncStorage.getItem('carbohydrateHeight');
if (carbohydrateHeightString !== null) {
setCarbohydrateHeight(parseFloat(carbohydrateHeightString));
} else {
setCarbohydrateHeight(0);
}
const proteinHeightString = await AsyncStorage.getItem('proteinHeight');
if (proteinHeightString !== null) {
setProteinHeight(parseFloat(proteinHeightString));
} else {
setProteinHeight(0);
}
const sugarHeightString = await AsyncStorage.getItem('sugarHeight');
if (sugarHeightString !== null) {
setSugarHeight(parseFloat(sugarHeightString));
} else {
setSugarHeight(0);
}
const caffeineHeightString = await AsyncStorage.getItem('caffeineHeight');
if (caffeineHeightString !== null) {
setCaffeineHeight(parseFloat(caffeineHeightString));
} else {
setCaffeineHeight(0);
}
const fatHeightString = await AsyncStorage.getItem('fatHeight');
if (fatHeightString !== null) {
setFatHeight(parseFloat(fatHeightString));
} else {
setFatHeight(0);
}
console.log('gleich');
}
} catch (error) {
console.error('Fehler beim Abrufen der Balkenhöhen: ', error);
}
};
getBalkenHeights();
}, []);
before I had my function like this and it worked:
// Hier rufen Sie die gespeicherten Balkenhöhen aus AsyncStorage beim Komponentenstart ab
useEffect(() => {
const getBalkenHeights = async () => {
try {
const sodiumHeightString = await AsyncStorage.getItem('sodiumHeight');
// Überprüfen, ob die Balkenhöhe bereits in AsyncStorage gespeichert ist
if (sodiumHeightString !== null) {
setSodiumHeight(parseFloat(sodiumHeightString));
} else {
// Wenn nicht, setzen Sie den Startwert auf 0
setSodiumHeight(0);
}
// Wiederholen Sie diesen Vorgang für alle Balkenhöhen
const magnesiumHeightString = await AsyncStorage.getItem('magnesiumHeight');
if (magnesiumHeightString !== null) {
setMagnesiumHeight(parseFloat(magnesiumHeightString));
} else {
setMagnesiumHeight(0);
}
const calciumHeightString = await AsyncStorage.getItem('calciumHeight');
if (calciumHeightString !== null) {
setCalciumHeight(parseFloat(calciumHeightString));
} else {
setCalciumHeight(0);
}
const potassiumHeightString = await AsyncStorage.getItem('potassiumHeight');
if (potassiumHeightString !== null) {
setPotassiumHeight(parseFloat(potassiumHeightString));
} else {
setPotassiumHeight(0);
}
const lithiumHeightString = await AsyncStorage.getItem('lithiumHeight');
if (lithiumHeightString !== null) {
setLithiumHeight(parseFloat(lithiumHeightString));
} else {
setLithiumHeight(0);
}
const boronHeightString = await AsyncStorage.getItem('boronHeight');
if (boronHeightString !== null) {
setBoronHeight(parseFloat(boronHeightString));
} else {
setBoronHeight(0);
}
const carbohydrateHeightString = await AsyncStorage.getItem('carbohydrateHeight');
if (carbohydrateHeightString !== null) {
setCarbohydrateHeight(parseFloat(carbohydrateHeightString));
} else {
setCarbohydrateHeight(0);
}
const proteinHeightString = await AsyncStorage.getItem('proteinHeight');
if (proteinHeightString !== null) {
setProteinHeight(parseFloat(proteinHeightString));
} else {
setProteinHeight(0);
}
const sugarHeightString = await AsyncStorage.getItem('sugarHeight');
if (sugarHeightString !== null) {
setSugarHeight(parseFloat(sugarHeightString));
} else {
setSugarHeight(0);
}
const caffeineHeightString = await AsyncStorage.getItem('caffeineHeight');
if (caffeineHeightString !== null) {
setCaffeineHeight(parseFloat(caffeineHeightString));
} else {
setCaffeineHeight(0);
}
const fatHeightString = await AsyncStorage.getItem('fatHeight');
if (fatHeightString !== null) {
setFatHeight(parseFloat(fatHeightString));
} else {
setFatHeight(0);
}
} catch (error) {
console.error('Fehler beim Abrufen der Balkenhöhen: ', error);
}
};
getBalkenHeights();
}, []);
