代码之家  ›  专栏  ›  技术社区  ›  lrr59

复制项目旁边的项目不会相应地工作

  •  1
  • lrr59  · 技术社区  · 10 月前

    我有一个具有以下格式的对象数组

    const [type1Options, setType1Options] = useState([
      {
        name: "Name1",
        value: "Value1",
      },
      {
        name: "Name2",
        value: "Value2",
      },
    ]);
    
    const [type2Options, setType2Options] = useState([
      {
        name: "Name1",
        value: "Value1",
      },
      {
        name: "Name2",
        value: "Value2",
      },
    ]);
    

    我使用每个条目的复制和删除按钮按类别呈现这些对象。Delete将从数组中删除条目,copy将把单击的项目内容复制到新条目中,并放置在单击的条目的正下方。删除操作很好,但复制最后一个条目并不能按预期工作。有人能帮忙吗?

    沙箱: https://codesandbox.io/p/sandbox/i18-demo-7594gf?file=%2Fsrc%2FApp.js%3A5%2C2-24%2C6

    用于复制和删除功能的实用程序

    export const deleteItems = (list, idx) => {
      const temp = [...list];
      temp.splice(idx, 1);
      return temp;
    };
    
    export const copyItems = (list, idx) => {
      const newItem = { ...list[idx] };
      const newItems = [...list.slice(0, idx + 1), newItem, ...list.slice(idx + 1)];
      return newItems;
    };
    
    import { useState } from "react";
    import List from "./List";
    
    export default function App() {
      const [type1Options, setType1Options] = useState([
        {
          name: "Name1",
          value: "Value1",
        },
        {
          name: "Name2",
          value: "Value2",
        },
      ]);
    
      const [type2Options, setType2Options] = useState([
        {
          name: "Name1",
          value: "Value1",
        },
        {
          name: "Name2",
          value: "Value2",
        },
      ]);
    
      return (
        <div>
          <List
            type1Options={type1Options}
            type2Options={type2Options}
            setType1Options={setType1Options}
            setType2Options={setType2Options}
          />
        </div>
      );
    }
    
    import Type1 from "./Type1";
    import Type2 from "./Type2";
    
    export default function List(props) {
      const { type1Options, type2Options, setType1Options, setType2Options } =
        props;
    
      return (
        <>
          <div>
            Category 1
            {type1Options.map((obj, index) => (
              <Type1
                index={index}
                obj={obj}
                type1Options={type1Options}
                setType1Options={setType1Options}
              />
            ))}
          </div>
          <br />
          <div>
            Category 2
            {type2Options.map((obj, index) => (
              <Type2
                index={index}
                obj={obj}
                type2Options={type2Options}
                setType1Options={setType2Options}
              />
            ))}
          </div>
        </>
      );
    }
    
    import "./styling.css";
    import { deleteItems, copyItems } from "./utils";
    
    export default function Type1(props) {
      const { index, obj, type1Options, setType1Options } = props;
    
      const copyHandler = () => setType1Options(copyItems(type1Options, index + 1));
    
      const deleteHandler = (index) =>
        setType1Options(deleteItems(type1Options, index + 1));
    
      return (
        <div className="box-container">
          <div className="box-header">
            <h3>Index {index + 1}</h3>
            <div className="box-buttons">
              <button onClick={copyHandler}>Copy</button>
              <button onClick={deleteHandler}>Delete</button>
            </div>
          </div>
          <div className="box-content">
            {obj.name}: {obj.value}
          </div>
        </div>
      );
    }
    
    1 回复  |  直到 10 月前
        1
  •  1
  •   Rohit Rajput    10 月前

    列表.jsx

    import Type1 from "./Type1";
    import Type2 from "./Type2";
    
    export default function List(props) {
      const { type1Options, type2Options, setType1Options, setType2Options } =
        props;
    
      return (
        <>
          <div>
            Category 1
            {type1Options.map((obj, index) => (
              <Type1
                index={index}
                obj={obj}
                type1Options={type1Options}
                setType1Options={setType1Options}
              />
            ))}
          </div>
          <br />
          <div>
            Category 2
            {type2Options.map((obj, index) => (
              <Type2
                index={index}
                obj={obj}
                type2Options={type2Options}
                setType2Options={setType2Options}
              />
            ))}
          </div>
        </>
      );
    }
    

    和类型2.jsx

    import React from "react";
    import "./styling.css";
    import { deleteItems, copyItems } from "./utils";
    
    export default function Type2(props) {
      const { index, obj, type2Options, setType2Options } = props;
    
      const copyHandler = () => setType2Options(copyItems(type2Options, index));
    
      const deleteHandler = () => setType2Options(deleteItems(type2Options, index));
    
      return (
        <div className="box-container">
          <div className="box-header">
            <h3>Index {index + 1}</h3>
            <div className="box-buttons">
              <button onClick={copyHandler}>Copy</button>
              <button onClick={deleteHandler}>Delete</button>
            </div>
          </div>
          <div className="box-content">
            {obj.name}: {obj.value}
          </div>
        </div>
      );
    }