你好,我正在try 根据我的数据创建一个数组,我使用了MongoDB,实际上是试图将_id添加到一个数组中.如果_id已经存在,那么它不会添加,我的意思是它不会重复,因为它是一个动态数据列表,我会循环,但当我单击添加数据时,它总是只为一个元素提供一个新的数组,而不是添加新的元素.我已经try 了很多方法,希望任何人都能帮忙.

Data:


[
  {
    _id: ObjectId("64d28f3abdc16189260a5521"),
    blood: "A+",
    birth: "2023-08-05",
    role: "student",
  },
  {
    _id: ObjectId("64d2edb9d0f86e45a48611b5"),
    blood: "B+",
    birth: "2013-08-05",
    role: "student",
  },
  {
    _id: ObjectId("64d2fd40d0f86e45a48611b8"),
    blood: "A+",
    birth: "2023-08-05",
    role: "student",
  },
  {
    _id: ObjectId("64d54434a2d26fwdsb83d12ab"),
    blood: "AB+",
    birth: "2013-06-05",
    role: "student",
  },
  {
    _id: ObjectId("64d48526b43hga0b83d12ab"),
    blood: "A+",
    birth: "2023-08-05",
    role: "student",
  },
  {
    _id: ObjectId("64d48526bd26fwdsb23sa12ab"),
    blood: "B+",
    birth: "2013-08-05",
    role: "student",
  },
  {
    _id: ObjectId("64d48343bd26faa0b83d12ab"),
    blood: "A+",
    birth: "2023-08-05",
    role: "student",
  },
  {
    _id: ObjectId("64d54434a2d26fwdsb83d12ab"),
    blood: "AB+",
    birth: "2013-06-05",
    role: "student",
  },
];

Code:

import React, { useState } from "react";
import { Col, Container, Row } from "react-bootstrap";
import "./ArrayManipulationComponent.css"; // Import your CSS file

const StudentGrid = ({ student }) => {
  const [elements, setElements] = useState([]);
  const { name, birth, _id } = student;

  const handleButtonClick = (_id) => {
    if (elements.includes(_id)) {
      const updatedElements = elements.filter(id => id !== _id);
      setElements(updatedElements);
    } else {
      setElements([...elements, _id]);
    }
  };

  console.log(elements);


  return (
    <div>
      <Container>
        <Row>
          <Col md={12}>
       
              <div>
              <p>
                <button
                  className={
                    elements.includes(_id) ? "active-button" : "action-button"
                  }
                  onClick={() => handleButtonClick(_id)}
                >
                  Button {_id}
                </button>
              </p>
            </div>
          </Col>
        </Row>
      </Container>
    </div>
  );
};

export default StudentGrid;

enter image description here

enter image description here

推荐答案

您已经声明了一个StudentGrid组件,该组件仅用于1个学生信息,但因此您正试图以某种方式更新学生列表.

这应该在父组件中完成.

所以我猜在您的父组件中有一个类似于以下代码的代码

students.map(studentInfo => <StudentGrid info={studentInfo} />

这是您应该放置所选元素的状态的位置

const [elements, setElements] = useState([]);

然后,当单击StudentGrid内的按钮时,您应该在父组件中处理该操作,其中完整的列表保持不变.

所以

           <button
              className={
                elements.includes(_id) ? "active-button" : "action-button"
              }
              onClick={() => props.handleButtonClick(_id)}
            >
              Button {_id}
            </button>

然后在您的父组件中进行类似的操作

<StudentGrid info={studentInfo} handleButtonClick={parentComponentHandleClick} />

Reactjs相关问答推荐

使用Overpass API Endpoint计算 map 上的面积

如何在单击行中的图标时避免选中ionic 复选框?

为多租户SSO登录配置Azure AD应用程序

NextJS如何正确保存添加到购物车中的产品会话

React:关于useEffect钩子如何工作的困惑

无法在 NextJS 中获取嵌套对象

从ReactDataGridtry 将数据传递给父组件

如何在 React 过滤器中包含价格过滤器逻辑?

在 reactJS 中导航时页面重新加载

即使配置了 webpack.config.js,html-loader 也不起作用

Formik onChange() 字段挂钩覆盖显示值和 Select 机制

从服务器获取数据时未定义,错误非法调用

为嵌套路由配置一个不存在的 url 的重定向

next js 13 在获取中使用标头时动态渲染而不是静态渲染?

动态添加或删除文本输入字段并将值设置为 React JS 中主要状态的数组

我应该使用 useEffect 来为 React Native Reanimated 的 prop 值变化设置动画吗?

Material UI:使用 mui 手风琴时出现props 类型失败:isValidElement 不是函数

自动重新获取不起作用 - RTK 查询

Storybook - 无法解析generated-stories-entry.cjs/字段browser不包含有效的别名配置

你如何使用 refs 访问 React 中映射子项的值?