【问题标题】:How to Reorder dynamic inputs in react?如何在反应中重新排序动态输入?
【发布时间】:2020-12-16 09:39:54
【问题描述】:

我是 React 新手,到目前为止我只学会了使用 hooks 编写代码。

我做了动态输入,用户可以根据需要添加任意数量的输入。 我想允许用户重新排列输入的顺序。

我尝试了一些代码(见下文),但它以警告告终。

警告:组件正在将受控输入更改为不受控制。

如何重新排列输入的顺序?

const Questions = ({ questionsData, setQuestionsData, title, id }) => {
  console.log(questionsData);
  const handleSave = async () => {
    if (id) {
      try {
        const response = await axios.post("http://localhost:3001/update", {
          id: id,
          title: title,
          questions: questionsData,
        });
        alert(response.data);
      } catch (error) {
        console.log(error.message);
      }
    } else if (!id && title !== "") {
      try {
        const response = await axios.post("http://localhost:3001/create", {
          title: title,
          questions: questionsData,
        });
        alert(response.data.message);
      } catch (error) {
        console.log(error.message);
      }
    } else {
      alert("Le Formulaire doit avoir un titre.");
    }
  };

  return (
    <>
      {questionsData.map((question, index) => {
        return (
          <div key={question + index} className="questions">
            <div className={question.type === "texte" ? "orange" : "red"}>
              <span>{question.type === "texte" ? "1" : "2"}</span>
              <img src={Minus} alt="" />
              <img
                src={question.type === "texte" ? FileWhite : StarWhite}
                alt=""
              />
            </div>
            <input
              type="text"
              placeholder="Ecrivez votre question"
              value={question.title}
              onChange={(event) => {
                let tab = [...questionsData];
                tab[index].title = event.target.value;
                tab[index].index = index;
                setQuestionsData(tab);
              }}
            ></input>
            <div>
              <img
                src={ChevronUp}
                alt="move up"
                onClick={() => {
                  let tab = [...questionsData];
                  let item = tab.splice(index, 1);
                  tab.splice(index + 1, 0, item);
                  setQuestionsData(tab);
                }}
              />
              <img src={ChevronDown} alt="move down" />
              <img
                src={SmallTrash}
                alt="delete question"
                onClick={() => {
                  let tab = [...questionsData];
                  tab.splice(index, 1);
                  setQuestionsData(tab);
                }}
              />
            </div>
          </div>
        );
      })}
      <div className="questionType">
        <div
          className="addText"
          onClick={() => {
            let tab = [...questionsData];
            tab.push({ type: "texte", title: "", index: "" });
            setQuestionsData(tab);
          }}
        >
          <img src={File} alt="" />
          <p>Ajouter une question "Texte"</p>
        </div>
        <div
          className="addNote"
          onClick={() => {
            let tab = [...questionsData];
            tab.push({ type: "note", title: "", index: "" });
            setQuestionsData(tab);
          }}
        >
          <img src={Star} alt="" />
          <p>Ajouter une question "Note"</p>
        </div>
      </div>
      <div className="save">
        <button onClick={handleSave}>Sauvegarder</button>
      </div>
    </>
  );
};

export default Questions;

【问题讨论】:

    标签: reactjs input dynamic


    【解决方案1】:

    您的上移逻辑添加到应该为index-1 的索引,如果index+1 小于或等于questions.length 并且index-1 大于0,请确保检查数组边界。我在沙盒中修复了上移问题。检查下面。

    https://codesandbox.io/s/eager-chatelet-ryo41?file=/src/Questions.js

    【讨论】:

    • 是的,抱歉,我在此处粘贴代码后更改了上移。我只是忘记了在我的 onclick 上下移动以将 [0] 添加到项目。我写了 tab.splice(index + 1, 0, item);而不是 tab.splice(index + 1, 0, item[0]);现在它完美地工作了。非常感谢您的帮助!
    • @FlorieAnstett 如果正确,请标记为已批准的答案。
    猜你喜欢
    • 2022-11-17
    • 2016-01-29
    • 2023-03-04
    • 2020-04-12
    • 2012-02-17
    • 2022-09-25
    • 1970-01-01
    • 2019-11-05
    • 2022-11-14
    相关资源
    最近更新 更多