我在Reaction中有一个返回html元素的函数

我正面临着一个奇怪的问题.当我安慰它时,我得到了错误的参数值

export default function App() {
  const [values, setValues] = useState({
    accordion1: [{ id: undefined, value: "" }],
    accordion2: [{ id: undefined, value: "" }]
  });

  const textJSX = (heading) => {
    const key = heading === "Accordion 1" ? "accordion1" : "accordion2";
    return (
      <TextField
        label="Enter Details"
        placeholder="Name, date and quote"
        size="small"
        fullWidth
        onChange={(e) => {
          console.log("key", key, heading);
          const temp = { ...values };
          temp[key] = [{ ...temp[key]?.[0], value: e.target.value }];
          setValues(temp);
        }}
        value={values?.[key]?.[0]?.value || ""}
      />
    );
  };

  const customImageJSX = (heading) => {
    const key = heading === "Accordion 1" ? "accordion1" : "accordion2";
    return (
      <div className="flex flex-col">
        {values[key]?.[0].id ? (
          <img className="h-12 w-12" src={values[key]?.[0].id} />
        ) : (
          <label htmlFor="attachment-button-file">
            <input
              className="hidden"
              id="attachment-button-file"
              // type="file"
              onClick={(e) => {
                console.log("key", key, heading);
                const temp = { ...values };
                temp[key] = [
                  {
                    ...(temp[key][0] || {}),
                    id: imgData
                  }
                ];
                setValues(temp);
              }}
            />
            <CloudUploadOutlinedIcon fontSize="large" />
          </label>
        )}
      </div>
    );
  };

  const acc = (options) => {
    const {
      defaultExpanded,
      heading,
      title1 = "Notes",
      title2 = "Image Load"
    } = options;
    return (
      <Accordion defaultExpanded={defaultExpanded}>
        <AccordionSummary expandIcon={<ExpandMoreIcon />}>
          <Typography
            color="InfoText"
            variant="h6"
            className="font-semibold"
            style={{ marginTop: "-10px" }}
          >
            {heading}
          </Typography>
        </AccordionSummary>
        <AccordionDetails style={{ marginTop: "-30px" }} className="">
          <Typography
            color="textSecondary"
            variant="body1"
            className="font-semibold text-lg mb-5 mt-10"
          >
            {title1}
          </Typography>
          {textJSX(heading)}

          <Typography
            color="textSecondary"
            variant="body1"
            className="font-semibold text-lg mb-5 mt-10"
          >
            {title2}
          </Typography>

          {customImageJSX(heading)}
        </AccordionDetails>
      </Accordion>
    );
  };

  return (
    <div className="App">
      {acc({ heading: "Accordion 1" })}
      {acc({ heading: "Accordion 2" })}
    </div>
  );
}

在这里,我用不同的标题调用acc函数两次.根据参数,我将决定需要存储在哪个对象中

当我在第二功能中做一些事情时(手风琴) 例如.在文本字段中,该文本字段位于extJSX中,它提供了正确的参数.但是如果我刺激图像加载,我得到的是第一个参数值,虽然我是在第二个函数(Accordion)中执行的,结果是图像在Accordion 1中显示,但预计在Accordion 2中显示

I have uploaded the above code in code sandbox. https://codesandbox.io/s/accordin-file-attachment-m86lwg.

记住在手风琴2中添加文本和图像,以查看我提到的问题

如有任何帮助,将不胜感激

推荐答案

您使用相同的ID两次,ID必须是唯一的

当您点击<label htmlFor="attachment-button-file">时,它会查找ID为attachment-button-file的第一个项目,因此它总是 Select 页面上的第一个项目

要解决此问题,请使用唯一ID:

<label htmlFor={`attachment-button-file-${key}`}>
<input
  className="hidden"
  id={`attachment-button-file-${key}`}
  { /* ... */ }
/>

Reactjs相关问答推荐

使用REACT-RUTER-DOM链接仅更新一个搜索参数

React Router:在没有手动页面刷新的情况下,路由不会更新内容

更改滑块标记的文本 colored颜色 ./Reaction/MUI 5

在transformResponse中使用来自其他查询的缓存

在Reaction中的第一次装载时,Use Effect返回空数组

useEffect firebase 清理功能如何工作?

设置自定义形状的纹理

React useContext 的未定义返回值

React-router动态路径

如何根据ReactJS中元素列表中的布尔值仅显示一次值

React Context API 在 Next.js 中更改路由时获取默认数据

如何在我的自定义主题中样式化MUI TreeItem组件

Spring Boot + React + MySQL应用的架构层面有哪些?

VideoSDK api 不使用更新状态

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

MUI - ButtonGroup fullWidth 条件屏幕大小不起作用?

如何在对话素材ui中设置边框半径?

用 jest 测试包裹在 redux 和 router 中的组件

如何使用 React Router 在 React 中正确同步 useEffect 和 useLocation 挂钩?

React Hooks 表单在日志(log)中显示未定义的用户名