React Native Asynch Storage

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();
    }, []);

error Parsing error: Unexpected character ‘‘’
there is a minor issue with the use of curly quotes (‘’ ) around the string in AsyncStorage.getItem('storedDate')

1 Like

I only see you getting/setting the date. When are you using resetBars and in what relation to the date is it used?

Also, it seems to have been removed.


I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.