1

below is what I have done

useEffect(() => {
        database()
        .ref(`/User/`)
        .on('value', function(snapshot) {
          const list = [];
          snapshot.val(doc => {
            const { name, email} = snapshot.val();
            list.push({
              id: doc.id,
              name,
              email,
            });
          });                



          
          setUsers(list);
          setLoading(false);
    
          console.log('User data: ', snapshot.val());
        });
  
    
        
    }, [userId]);

   

it shows the information from the database on the console, but it doesn't show on the app, doing this for firebase firestore works fine, but its not loading any information to the app page

below is how I have used flatlist and list.item tags to show the data from the database in the app

<FlatList
data={users}
ItemSeparatorComponent={() => <Divider />}
keyExtractor={item => item.id}
renderItem={({ item }) => (
  <TouchableOpacity
    onPress={() => navigation.navigate('Room', { thread: item })}
  >
    <List.Item
      title={item.name}
      description={item.email}
      titleNumberOfLines={1}
      titleStyle={styles.listTitle}
      descriptionStyle={styles.listDescription}
      descriptionNumberOfLines={1}
    />
  </TouchableOpacity>
)}

/>

I am using the latest versions of react-native and firebase

2 Answers 2

1

You are not supposed to pass a callback to snapshot.val() again.

It should be :

ref.once('value', function(snapshot) {
  snapshot.forEach(function(userSnapshot) {
    var userKey = userSnapshot.key;
    var userData = userSnapshot.val();
    // userData['id'] = userKey;
    users.push(userData); 
  });
});

You can also try this library https://github.com/CSFrequency/react-firebase-hooks.

Sign up to request clarification or add additional context in comments.

4 Comments

Good catch. 1) It's snapshot.forEach indeed, not snapshot.val and 2) you'll want to get the val() of the iterator, as otherwise you're getting /User/name and /User/email every time.
users.push(id, userData); this is what worked, thanks for the clarification 😊😊
anytime the list updates, it forms another array that adds to the previous array, how can i make it to reurn just one array with the updated information
@RaphaelInyang you can empty the array before snapshot.forEach ; Something like users = []
0
 database()
          .ref(`/User/`)
          .once('value', function(snapshot) {  snapshot.forEach(function(userSnapshot) {
const id = userSnapshot.key;
const userData = userSnapshot.val();
  userData['id'] = id;
users.push(id, userData);  });});

this is what worked, the flatlist requires an id to be passed, thanks so much, those who helped out 😊😊

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.