Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
Update only specific data in Realtime database of Firebase v9

I am new to firebase and using ReactJs, I am wondering how can I update a specific data in Realtime database, I want to change the value of it to true or false interchangeably.

Image

This is my code.

  const [status, setStatus] = useState(false);
  const [tempId, setTempId] = useState("");
  const [todo, setTodo] = useState("");
  
  const updateStatus = () => {
    update(ref(db, `/${auth.currentUser.uid}/${tempId}`), {
      completed: setStatus(!status),
      todo: todo,
      id: tempId,
    });
  };

Thank you in Advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you only want to update the status property, you can do:

update(ref(db, `/${auth.currentUser.uid}/${tempId}`), {
  completed: !status
});

Since your calling update, it will leave the other properties of the node unmodified.


Alternatively, you can do:

update(ref(db, `/${auth.currentUser.uid}/${tempId}/status`), !status)

Or:

set(ref(db, `/${auth.currentUser.uid}/${tempId}/status`), !status)

Both of these write to the complete path of just the status property.


What you'll note is that I don't call setStatus in the code above. Instead I recommend listening to the value of the status node, and then calling setStatus when you get a callback there:

onValue(ref(db, `/${auth.currentUser.uid}/${tempId}/status`), (snapshot) => {
  setStatus(snapshot.val());
});

Separating the read and write operations like this is known as command query responsibility segregation (or CQRS) and in the case of Firebase won't even be slower for the user, as the local SDK will immediately call the onValue handler when you call set or update on that path.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!