reactjs - 即使只有一个文档被添加到 firebase,也会显示重复的任务
问题描述
我正在使用 react-redux-firebase 创建任务管理器。创建任务时,用户填写表格,此数据用于将文档添加到我在 firebase 中的待办事项集合中。所以我们为每个待办事项都有一个文档。然后另一个组件将 todo 集合中的每个文档映射到一张卡片,以便用户可以看到每个 todo
当我们创建任务时,问题就出现了,它在我们的待办事项部分下显示了一个重复的任务。然而,只有一个新文档被添加到我们在 firebase 中的待办事项集合中。此外,一旦我刷新页面,重复的任务就会消失。即使在 firebase 中只创建了一个新文档,我也完全不明白为什么会显示重复的任务。什么会导致副本在那里开始?任何帮助都感激不尽!
希望这两张图片能帮助你更好地理解这个问题,因为我发现解释起来有点困难。
副本示例:
当我刷新页面时,重复项消失:
这是将表单提交到 firebase 的函数:
handleCreateSubmit = e => {
e.preventDefault();
const { firestore } = this.props;
this.props.users.forEach(user => {
if (user.uid === this.props.auth.uid) {
const dueDate = new Date(this.state.due + " " + this.state.eta);
const newTask = {
title: this.state.title,
description: this.state.description,
due: dueDate,
createdBy: user.name,
status: "todo",
department: this.state.departmentName
};
firestore
.add({ collection: "todo" }, newTask)
.then(() => console.log("task added"))
.catch(() => {
console.log("error");
});
this.setState({
title: "",
description: "",
due: "",
eta: ""
});
this.addCreateNotification(user.name);
} else if (!user.uid === this.props.auth.uid) {
return null;
}
});
this.handleCreateClose();
};
这是将每个文档映射到卡片的函数:
ren = () => {
if (this.props.todo) {
return (
<React.Fragment>
{this.props.todo.map((item, i) => {
if (item.department === this.props.match.params.id) {
return (
<div key={i}>
<div className="card" style={{ backgroundColor: "#d71636"
}}>
<div className="card-content white-text">
<span className="card-title">
{item.title}{" "}
<Start
id={item.id}
className="start"
style={{ float: "right" }}
onClick={e => {
this.handleClickOpen(e);
}}
/>
<Delete
id={item.id}
className="start"
style={{ float: "right" }}
onClick={e => {
this.delete(e);
}}
/>
</span>
<p>
<b>Created By: </b>
{item.createdBy}
</p>
<p>
<b>Due:</b> {moment(item.due.toDate()).calendar()}
</p>
<p>
<b>Description: </b>
{item.description}
</p>
</div>
</div>
</div>
);
}
})}
</React.Fragment>
);
} else {
return <Loading />;
}
};
解决方案
推荐阅读
- python - Tkinter:如何使用单独的窗口通过按钮要求用户输入并将该值返回给原始函数
- java - 使用 oauth2Login 添加基本身份验证
- api - 在 API-REST/Kubernetes 场景中,如何以面向消息代理的方式处理订阅中的事件传入?
- c++ - 在 C++ 中,调用派生构造函数是否会创建一个基对象?
- python - 二进制 DataFrame 到字典
- mongodb - 码头工人 | 姜戈 | MongoDB 困境
- python - 如何从客户端服务器获取数据
- android - Android:新的 BufferedReader(新的 FileReader - 导致捕获错误
- continuous-integration - npm 错误!git@github.com:权限被拒绝(公钥)。在 GitHub 操作中
- jekyll - 网站没有在 jekyll 中呈现我的内容