简体   繁体   English

使用 firebase 数据库在 ReactJs 中的完整按钮上切换真/假 state

[英]Toggle true/false state on complete button in ReactJs using firebase database

I am building to do list and I have complete button where I want to toggle true or false state and updating in firebase.我正在构建待办事项列表,并且我有完整的按钮,我想在其中切换真或假 state 并在 firebase 中更新。 I was searching on the platform, there were simillar solutions but not exacly what I needed.我在平台上搜索,有类似的解决方案,但不是我需要的。

So far I have this function:到目前为止,我有这个 function:

const completedHandler = (id) => {
const docRef = doc(db, "todos", id);
updateDoc(docRef, {
  isCompleted: !initialValues.isCompleted
})
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error.message);
  });
};

The problem is that this is updating the state only once, and it won't change it again, for example if the state is false, it is updating in firebase as true, and that's it.问题是这仅更新 state 一次,并且不会再次更改它,例如,如果 state 为假,它正在 firebase 中更新,并且它是真的。 I am not sure where is the issue.我不确定问题出在哪里。 Any help is appreciate.任何帮助表示赞赏。

Since the value that you write to the database depends on the current value of the field in the database, you'll need to first read the data and then write the new value based on that.由于您写入数据库的值取决于数据库中字段的当前值,因此您需要首先读取数据,然后根据该数据写入新值。 The best way to do this is with a transaction and would look something like:最好的方法是使用事务,看起来像:

import { runTransaction } from "firebase/firestore";

try {
  const docRef = doc(db, "todos", id);
  await runTransaction(db, async (transaction) => {
    const todoDoc = await transaction.get(docRef);
    if (!todoDoc.exists()) {
      throw "Document does not exist!";
    }

    const newValue = !todoDoc.data().isCompleted;
    transaction.update(docRef, { isCompleted: newValue });
  });
  console.log("Transaction successfully committed!");
} catch (e) {
  console.log("Transaction failed: ", e);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 实时数据库规则自动从true变为false如何解决? - the Firebase Realtime database rules are changing from true to false automatically how to fix it? 如果不同的条件为真,Firebase 如何打破具有不同状态/消息的实时数据库事务? - Firebase how to break realtime database transaction with different state/message if different condition is true? Firebase 实时数据库 orderByChild 数据结构 - ReactJS - Firebase Realtime Database orderByChild data structure - ReactJS reactjs 和 firebase function db(数据库)无法运行 - reactjs and firebase function db (database) not working on running 检索没有“真”的 Firebase 数据库值 - Retrieve Firebase database values without 'true' 如何在 Flutter 中用 boolean 值(真假)对来自 firebase 的数据进行排序? - How to sort data from firebase with boolean value (true and false) in Flutter? 如何使用 reactjs 从 firebase 数据库中获取单个数据值 - How to fetch a single data value out of firebase database with reactjs 在 REACTjs 中使用 onSnapshot Firebase 时出现 memory 错误 - Out of memory error on using onSnapshot Firebase in REACTjs 无法使用 ReactJS 中的 firebase 身份验证重新发送 OTP - Not able to resend OTP using firebase auth in ReactJS 使用 Firebase 身份验证与 mongodb 数据库 - Using Firebase Authentication with mongodb database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM