我有一个动态手风琴.我正在从后端接收消息.但每个手风琴上都印着.我正在共享代码

import React, { useState, useEffect } from "react";

import ApplicantDash from "./ApplicantDash";
import {
  Accordion,
  AccordionSummary,
  AccordionDetails,
  Typography,
} from "@material-ui/core";
import * as FcIcons from "react-icons/fc";

import ApplicantService from "../services/ApplicantService";

export default function AvailJobs() {
  const [aplcntEmail, setAplcntEmail] = useState("aman@gmail.com"); //change to aplcntemail
  const [isShow, setIsShow] = useState(false);
  const [msg, setMsg] = useState([""]);
  const [job, setJob] = useState([
    {
      jobTitle: "",
      dateOfPosting: Date,
      lastDateToApply: new Date().toLocaleDateString([], {
        year: "numeric",
        month: "long",
        day: "numeric",
      }),
      preferableSkills: [],
      requiredExp: 0,
      recruiterEmail: "",
      companyName: "",
      companyAddress: "",
      
    },
  ]);

  useEffect(() => {
    const data = ApplicantService.getAllJobs()
      .then((response) => {
        console.log(response.data);
        setJob(response.data);
      })
      .catch((error) => {
        alert(error.response.data);
      });
  }, []);

  const onApplyButton = (item,key) => {
     const data2 = ApplicantService.applyForJob(aplcntEmail, item)
      .then((response) => {
        console.log(response.data);
        setIsShow(true);
        setMsg(response.data)
    
      })
      .catch((error) => {
        setIsShow(true);
        setMsg(error.response.data);
        
      });
  };
  return (
    <div>
      <ApplicantDash />
      <div className="container bg-light">
        <div className="card-bodies">
          <section className="mb-4">
            <h2 className="h1-responsive font-weight-bold text-center my-4">
              All Available jobs
            </h2>
          </section>
          {job.map((item, key) => (
            <>
            
              <Accordion key={key}>
                <AccordionSummary
                  expandIcon={<FcIcons.FcExpand />}
                  aria-controls="panel1a-content"
                  id="panel1a-header"
                  className="Accordian"
                >
                  <Typography>
                    <div className="d-flex p-1 justify-content-evenly">
                      <div className="p-1">
                        <b> Job: </b> {item.jobTitle}
                      </div>
                      <div className="p-2"></div>
                      <div className="p-1">
                        <b> Company: </b> {item.companyName}
                      </div>
                      <div className="p-2"></div>
                      <div className="p-1">
                        <b> Last Date: </b> {item.lastDateToApply}
                      </div>
                    </div>
                  </Typography>
                </AccordionSummary>
                <AccordionDetails>
                  <Typography>
                    <div className="container">
                      <table class="table table-borderless">
                        <tbody>
                          <tr>
                            <td>JOB TITLE</td>
                            <td>:</td>
                            <td>
                              <b>{item.jobTitle}</b>
                            </td>
                          </tr>
                          <tr>
                            <td>Company</td>
                            <td>:</td>
                            <td>
                              <b>{item.companyName}</b>
                            </td>
                          </tr>
                          <tr>
                            <td>Address</td>
                            <td>:</td>
                            <td>
                              <b>{item.companyAddress}</b>
                            </td>
                          </tr>
                          <tr>
                            <td>Last Date to Apply</td>
                            <td>:</td>
                            <td>
                              <b>{item.lastDateToApply}</b>
                            </td>
                          </tr>
                          <tr>
                            <td>Experience</td>
                            <td>:</td>
                            <td>
                              <b>{item.requiredExp}</b>
                            </td>
                          </tr>
                          <tr>
                            <td> Skills </td>
                            <td>:</td>
                            <td>
                              <table className="table table-condensed w-auto table-borderless table-hover">
                                {item.preferableSkills.map((S, index1) => {
                                  return (
                                    <tbody key={index1}>
                                      <td scope="col">
                                        {index1 + 1}.<b>{S}</b>
                                      </td>
                                    </tbody>
                                  );
                                })}
                              </table>
                            </td>
                          </tr>
                          <tr>
                            <td></td>
                            <td></td>
                            <td>
                              <button
                                type="button"
                                class="btn btn-primary"
                                onClick={() => onApplyButton(item,key)}
                              >
                                Apply for the job{" "}
                              </button>
                            </td>
                          </tr>
                        </tbody>
                          {isShow && <> 
                            {msg}
                         </>}
                      </table>
                    </div>
                  </Typography>
                </AccordionDetails>
              </Accordion>
            </>
          ))}
        </div>
      </div>
    </div>
  );
}

现在,当我点击"申请这份工作"按钮时.我从后端收到的消息仅打印到活动的accordion

enter image description here

正如您所看到的,后端的响应是在手风琴的两个

推荐答案

Issue

这里的问题是,您有一个布尔isShow状态和一个msg状态,104手风琴细节部分使用相同的单个isShow状态有条件地渲染msg状态.

Solution

一个简单的解决方案是存储手风琴的id、标题或索引,以显示的消息.

示例:

export default function AvailJobs() {
  ...

  const [isShow, setIsShow] = useState({}); // <-- initially empty object

  ...

  const onApplyButton = (item, key) => {
    ApplicantService.applyForJob(aplcntEmail, item)
      .then((response) => {
        console.log(response.data);
        setMsg(response.data);
      })
      .catch((error) => {
        setMsg(error.response.data);
      })
      .finally(() => {
        setIsShow(show => ({
          ...show,
          [key]: true // <-- set true the specific key
        }));
      });
  };

  return (
    <div>
      ...
          {job.map((item, key) => (
            <Accordion key={key}>
              ...
              <AccordionDetails>
                <Typography>
                  <div className="container">
                    <table class="table table-borderless">
                      <tbody>
                        ...
                        <tr>
                          ...
                          <td>
                            <button
                              type="button"
                              class="btn btn-primary"
                              onClick={() => onApplyButton(item, key)}
                            >
                              Apply for the job
                            </button>
                          </td>
                        </tr>
                      </tbody>
                      {isShow[key] && <>{msg}</>} // <-- check if isShow[key] is truthy
                    </table>
                  </div>
                </Typography>
              </AccordionDetails>
            </Accordion>
          ))}
      ...
    </div>
  );
}

Reactjs相关问答推荐

我想将状态设置为true,直到每一张卡片都生成并在多姆中呈现

如何避免react 使用ESLINTreact 挂钩/穷举-deps进行第一次呈现

从Microsoft Excel粘贴复制的单元格时,将Excel单元格格式(粗体、下划线、斜体)带入Reaction

以下代码是否存在安全问题?

REACTJS:在Reaction中根据路由路径更改加载器API URL

如何访问子集合中的文档?

如何在整个应用程序中显示通用弹出窗口中的点击事件

根据处于react 状态的数组的名称键,将新对象添加到嵌套的对象数组中

如何在一个 Next.js 项目中使用两种布局?

在React中使用if条件时如何更改Tailwind中元素的文本 colored颜色

将 useRef 与 useImperativeHandle 一起使用时,React 不会重新渲染

useEffect 内的 onClick 和 addEventListener 之间的 click 事件顺序差异

如何在屏幕上使用相同的可重用

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

如何通过 id 从 useSprings 数组中删除元素

ReactJs 行和列

如何使图标适合 react-bootstrap 中的行元素

如何在 Cypress E2E 的站点上测试react 组件?

如何将本地视频文件上传到 Google Cloud

错误未捕获的错误:[App] 不是 组件. 的所有子组件必须是