0

**Im making a book tracking app that keeps giving me errors regarding a property/variable, even tho it has already been declared. I havent been successfull in trying new variables and think its an issue with react-native, as a few of my collegues are having the same issue. Any help would be amazing and I would be very thankful for any input. **

import {Button, View, Text, StyleSheet} from 'react-native';
import { useState, useEffect } from 'react'; 
import AsyncStorage from '@react-native-async-storage/async-storage';
import { useIsFocused } from "@react-navigation/native";

var someArray=[]; 
let sum = 0; 
let average = 0; 
let count = 0; 

function HomeScreen({navigation}){
  const [thetitle, setTheTitle] = useState('');
  const [theauthor, setTheAuthor] = useState('');
  const [thenumpgs, setTheNumpgs] = useState(0);
  const [thegenre, setTheGenre] = useState('');
  const isFocused = useIsFocused();

  useEffect(() => { 
    if(isFocused){ 
      AsyncStorage.getItem('@theWholeBookList').then(
        (value) => anotherRandomArray = JSON.parse(value)
      ); 
      setTheTitle(anotherRandomArray[anotherRandomArray.length -1]?.title); 
      setTheAuthor(anotherRandomArray[anotherRandomArray.length -1]?.author);
      setTheNumpgs(anotherRandomArray[anotherRandomArray.length -1]?.numpgs);
      setTheGenre(anotherRandomArray[anotherRandomArray.length -1]?.genre);
      
      someArray = [...anotherRandomArray];  
      for (count = 0; count < someArray.length; count++){
        sum = sum + Number(someArray[count].numpgs); 
      }
      average = sum / someArray.length;
    }
   
  }, [isFocused]);

  return (
    <View style={styles.container}>
      <View style={styles.paragraph}>
        <Text style={styles.paragraph}>App</Text>
        <Text style={styles.paragraph}>LBR</Text>
        <Text>Title - {thetitle}</Text>
        <Text>Author - {theauthor}</Text>
        <Text>Pages - {thenumpgs}</Text>
        <Text>Genre - {thegenre}</Text>
        <Text>Total - {sum}</Text>
        <Text>Average - {average}</Text> 
      </View>
      <Button
        title="Go to EnterBook"
        onPress={() => navigation.navigate('EnterBookScreen')}
      />
      <Button
        title="Go to History"
        onPress={() => navigation.navigate('HistoryScreen')}
      />
      <Button
        title="Go to Genre"
        onPress={() => navigation.navigate('GenreScreen')}
      />
      <View>
      <Text style={styles.otherText}>The Content of the Array</Text>
      {someArray.map((book) => {
        return (
          <View>
            <Text style={styles.item}>{book.title} - {book.author} - {book.numpgs} - {book.genre}</Text>
          </View>
        );
      })}
      </View>
    </View>
  );
}


export default HomeScreen;
[Emulator showing the errors I have been reciveing][1]```


  [1]: https://i.stack.imgur.com/8xVgp.png
  • 1
    Where is the declaration fo `anotherRandomArray`? – Konrad Nov 23 '22 at 11:12
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Konrad Nov 23 '22 at 11:12

0 Answers0