reactjs - 如何从 mongoDB 中检索数据并显示在可拖放的待办事项列表中?
问题描述
我已经实现了一个可拖放的待办事项列表的代码。
const item = {
id: v4(),
name: "Clean the house"
}
在上面的代码中,我需要存储在 mongoDB 中的 stageName 来代替“name”。我已经实现了redux检索代码,我只需要知道每次从数据库循环时如何创建一个新的常量变量。可拖放待办事项列表的完整代码如下:
import React, {useState} from 'react';
import './App.css';
import {DragDropContext, Droppable, Draggable} from "react-beautiful-dnd";
import _ from "lodash";
import {v4} from "uuid";
const item = {
id: v4(),
name: "Clean the house"
}
const item2 = {
id: v4(),
name: "Wash the car"
}
function App() {
const [text, setText] = useState("")
const [state, setState] = useState({
//creating 3 columns
"todo": {
title: "Todo",
items: [item, item2]//temporary data valuesa
},
"in-progress": {
title: "In Progress",
items: []
},
"done": {
title: "Completed",
items: []
}
})
const handleDragEnd = ({destination, source}) => {
if (!destination) {
return
}
if (destination.index === source.index && destination.droppableId === source.droppableId) {
return
}
// Creating a copy of item before removing it from state
const itemCopy = {...state[source.droppableId].items[source.index]}
setState(prev => {
prev = {...prev}
// Remove from previous items array
prev[source.droppableId].items.splice(source.index, 1)
// Adding to new items array location
prev[destination.droppableId].items.splice(destination.index, 0, itemCopy)
return prev
})
}
const addItem = () => {
setState(prev => {
return {
...prev,
todo: {
title: "Todo",
items: [
{
id: v4(),
name: text
},
...prev.todo.items
]
}
}
})
setText("")
}
//the dragdropcontext is the wrapper for draging elements
//dropable is the certain area inside the column where u can drop items
//dragble are the items in the column to be dragged
//here {_.map(state, (data, key) => {
//the above function is used to return the data and key of all 3 elements mentioned under use state() above
//the key: returns todo,inprogress,and done
//where as the data returns all the values of the variables within each key
return (
<div className="App">
<div>
<input type="text" value={text} onChange={(e) => setText(e.target.value)}/>
<button onClick={addItem}>Add</button>
</div>
<DragDropContext onDragEnd={handleDragEnd}>
{_.map(state, (data, key) => {
return(
<div key={key} className={"column"}>
<h3>{data.title}</h3>
<Droppable droppableId={key}>
{(provided, snapshot) => {
return(
<div
ref={provided.innerRef}
{...provided.droppableProps}
key = {"Todo"}
className={"droppable-col"}
>
{data.items.map((el, index) => {
return(
<Draggable key={el.id} index={index} draggableId={el.id}>
{(provided, snapshot) => {
console.log(snapshot)
return(
<div
className={`item ${snapshot.isDragging && "dragging"}`}
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
>
{el.name}
</div>
)
}}
</Draggable>
)
})}
{provided.placeholder}
</div>
)
}}
</Droppable>
</div>
)
})}
</DragDropContext>
</div>
);
}
export default App;
解决方案
推荐阅读
- windows - 使用“1”参数调用“SetAccessRule”的异常:“无法翻译部分或全部身份引用。”
- python - CSV到使用python的结构化嵌套JSON
- swiftui - 嵌套的 ForEach + ScrollView Header 导致故障
- python - 如何检测熊猫整数序列之间的间隙?
- java - 来自 Java BlockingQueue 的工作项的并发有序处理
- google-apps-script - 我结合了两个 onedit 脚本,只有一个在工作
- python - 如何在 Windows 上清理 python print() 输出以进行管道?
- regex - 正则表达式验证
- python - 如何在 Python 中使用 grequest 为多个 REST 发布请求传递不同的有效负载
- angular - Angular:解码编码的 UNICODE 字符串以打印符号