0
0
React Nativemobile~20 mins

Realtime Database in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Realtime Database Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Displaying Realtime Data Updates
You have a React Native app connected to Firebase Realtime Database. Which code snippet correctly listens for changes to the 'messages' node and updates the state to display new messages in real time?
React Native
import { useEffect, useState } from 'react';
import { database } from './firebaseConfig';
import { ref, onValue } from 'firebase/database';

function Messages() {
  const [messages, setMessages] = useState([]);

  useEffect(() => {
    const messagesRef = ref(database, 'messages');
    const unsubscribe = onValue(messagesRef, (snapshot) => {
      const data = snapshot.val();
      if (data) {
        setMessages(Object.values(data));
      } else {
        setMessages([]);
      }
    });
    return () => unsubscribe();
  }, []);

  return null;
}
A
useEffect(() => {
  const messagesRef = ref(database, 'messages');
  const unsubscribe = onValue(messagesRef, (snapshot) => {
    setMessages(snapshot.val());
  });
  return () => unsubscribe();
}, []);
B
useEffect(() => {
  const messagesRef = ref(database, 'messages');
  const unsubscribe = onValue(messagesRef, (snapshot) => {
    const data = snapshot.val();
    if (data) {
      setMessages(Object.values(data));
    } else {
      setMessages([]);
    }
  });
  return () => unsubscribe();
}, []);
C
useEffect(() => {
  const messagesRef = ref(database, 'messages');
  onValue(messagesRef, (snapshot) => {
    setMessages(snapshot.val());
  });
}, []);
D
useEffect(() => {
  const messagesRef = ref(database, 'messages');
  const unsubscribe = onValue(messagesRef, (snapshot) => {
    const data = snapshot.val();
    setMessages(data ? Object.keys(data) : []);
  });
  return () => unsubscribe();
}, []);
Attempts:
2 left
💡 Hint
Remember to convert the snapshot object to an array of values before setting state.
🧠 Conceptual
intermediate
1:30remaining
Understanding Realtime Database Data Structure
In Firebase Realtime Database, why is it recommended to structure data as a flat JSON tree rather than deeply nested objects?
ABecause flat structures reduce the chance of data duplication and improve query performance.
BBecause deeply nested objects are automatically converted to flat structures by Firebase.
CBecause flat structures allow simultaneous writes to multiple unrelated nodes in one operation.
DBecause Firebase Realtime Database does not support nested objects at all.
Attempts:
2 left
💡 Hint
Think about how Firebase downloads and listens to data changes.
lifecycle
advanced
1:30remaining
Cleaning Up Firebase Listeners in React Native
What is the correct way to remove a Firebase Realtime Database listener in a React Native functional component to avoid memory leaks?
React Native
useEffect(() => {
  const refMessages = ref(database, 'messages');
  const unsubscribe = onValue(refMessages, (snapshot) => {
    // update state
  });
  // What should be returned here?
}, []);
Areturn () => onValue(refMessages, null);
Breturn unsubscribe;
Creturn () => unsubscribe();
DNo return needed; Firebase cleans up automatically.
Attempts:
2 left
💡 Hint
React expects a cleanup function from useEffect to run on unmount.
📝 Syntax
advanced
2:00remaining
Correct Syntax for Writing Data to Realtime Database
Which code snippet correctly writes a new message object to the 'messages' node in Firebase Realtime Database using React Native?
React Native
import { ref, push, set } from 'firebase/database';

function sendMessage(database, message) {
  // Fill in the code
}
Aset(ref(database, 'messages'), message);
B
const messagesRef = ref(database, 'messages');
push(messagesRef, message);
C
const newMessageRef = push(ref(database, 'messages'));
set(newMessageRef, message);
D
const messagesRef = ref(database, 'messages');
const newMessageRef = push(messagesRef);
set(newMessageRef, message);
Attempts:
2 left
💡 Hint
Use push(ref) to generate a new child reference with a unique key, then set(newRef, message) to write the data.
🔧 Debug
expert
2:30remaining
Debugging Missing Realtime Updates in React Native
A React Native app using Firebase Realtime Database does not update the UI when data changes, even though the database has new data. Which is the most likely cause?
AThe onValue listener callback updates state with snapshot.val() but the state variable is not used in the render function.
BThe onValue listener is set up inside useEffect but the cleanup function removes it immediately after setup.
CThe database rules prevent read access, so onValue never triggers.
DThe app uses onChildAdded instead of onValue, so it only listens for new children, not updates.
Attempts:
2 left
💡 Hint
Check if the state updated by the listener is actually used to render UI.