删除状态映射中的特定字段总是删除它的最后一个元素

发布于 2025-01-09 11:55:23 字数 1830 浏览 1 评论 0原文

我在我的功能组件中使用 useState 定义一个状态,如下所示:

 const [appointmentList, setAppointmentList] = useState([]);

该状态代表一个表单列表,我可以像这样添加任意数量的:

const addAppointment = useCallback(() => {
  setAppointmentList([...appointmentList, {id: uuid()}]);
}, [appointmentList]);

但是在删除约会时我遇到了问题。为此,我使用这个 useCallback :

const removeAppointment = useCallback((i) => {
  let newAppointmentList = [...appointmentList];
  newAppointmentList.splice(i, 1);
  setAppointmentList(newAppointmentList)
}, [appointmentList]);

删除该州中正确的约会效果很好,但它总是删除我的应用程序视图中的最后一个约会。

例如,我有 2 个填写了值的表单。我单击删除第一个表单,在我的状态下,我现在只获得了appointmentList状态中第二个表单的值,但在我看来,最后一个表单已被删除,所以我仍然得到了第一个表单及其值...

我认为问题可能来自这样的事实:我的函数仅删除状态中的正确约会,然后地图意识到约会列表作为减少,然后地图呈现最近的约会而不是选择要删除的约会。

我的表单如下所示:

return(
  <div>
    {appointmentList.map((appointment, index) => (
      <Row key={`${index}`} id={index}>
            <Col>
              <Field
                name={`appointments[${index}].title`}
                id="title"
                component={InputTextField}
                value={appointment.title || ""}
                onChange={e => handleChange(index, e)}
              />
              <Field
                manyOther field
              />
             <Button
                 theme={BUTTON_THEMES.SECONDARY}
                 size={BUTTON_SIZES.SMALL}
                 onClick={() => removeAppointment(index)}
                >
           </Col>
       </Row>
    ))}
    <div>
      <Button
         theme={BUTTON_THEMES.SECONDARY}
         size={BUTTON_SIZES.SMALL}
         onClick={addAppointment}
       >
    </div>
 </div>

那么我怎样才能删除正确的表单呢?

谢谢您的帮助=)

I have in my functionnal component define a state with useState like that :

 const [appointmentList, setAppointmentList] = useState([]);

This state reprensent a list of form, i can add as many as i want like this :

const addAppointment = useCallback(() => {
  setAppointmentList([...appointmentList, {id: uuid()}]);
}, [appointmentList]);

But i got a probleme when it's come to delete the appointments. To do it, i use this useCallback :

const removeAppointment = useCallback((i) => {
  let newAppointmentList = [...appointmentList];
  newAppointmentList.splice(i, 1);
  setAppointmentList(newAppointmentList)
}, [appointmentList]);

It's work well to delete the correct appointment in the state but it's always delete the last appointment in the view of my application.

For exemple, i have 2 form filled with values. I click to delete the first form, in my state i now only got the values of the second form in the appointmentList state, but in my view, the last form as been deleted so i still got the first form with his values...

I think the probleme might come from the fact that my function only erase the correct appointment in the state, then the map realise that the appointmentList as reduce and then the map render the most recent appointment not the on selected to be deleted.

My Form look like this :

return(
  <div>
    {appointmentList.map((appointment, index) => (
      <Row key={`${index}`} id={index}>
            <Col>
              <Field
                name={`appointments[${index}].title`}
                id="title"
                component={InputTextField}
                value={appointment.title || ""}
                onChange={e => handleChange(index, e)}
              />
              <Field
                manyOther field
              />
             <Button
                 theme={BUTTON_THEMES.SECONDARY}
                 size={BUTTON_SIZES.SMALL}
                 onClick={() => removeAppointment(index)}
                >
           </Col>
       </Row>
    ))}
    <div>
      <Button
         theme={BUTTON_THEMES.SECONDARY}
         size={BUTTON_SIZES.SMALL}
         onClick={addAppointment}
       >
    </div>
 </div>

So how can i delete the correct form ?

Thank you for your help = )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧伤慢歌 2025-01-16 11:55:23

这是一个工作演示。你们的代码相似吗?因为我的正在工作。

Codesandbox

import { useState } from "react";
import "./styles.css";

export default function App() {
  const [appointmentList, setAppointmentList] = useState([1, 2, 3, 4, 5]);

  const addAppointment = () => {
    setAppointmentList([...appointmentList, { id: uuid() }]);
  };

  const removeAppointment = (i) => {
    let newAppointmentList = [...appointmentList];
    newAppointmentList.splice(i, 1);
    setAppointmentList(newAppointmentList);
  };

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <div style={{ display: "flex", flexDirection: "column" }}>
        {appointmentList.map((item, index) => {
          return <div key={item.id}> {item.id} <button onClick={() => removeAppointment(index)}>delete</button></div>;
        })}
      </div>
      <button onClick={addAppointment} >Add</button>
    </div>
  );
}

This is a working demo. Are your codes similar? Because mine are working.

Codesandbox

import { useState } from "react";
import "./styles.css";

export default function App() {
  const [appointmentList, setAppointmentList] = useState([1, 2, 3, 4, 5]);

  const addAppointment = () => {
    setAppointmentList([...appointmentList, { id: uuid() }]);
  };

  const removeAppointment = (i) => {
    let newAppointmentList = [...appointmentList];
    newAppointmentList.splice(i, 1);
    setAppointmentList(newAppointmentList);
  };

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <div style={{ display: "flex", flexDirection: "column" }}>
        {appointmentList.map((item, index) => {
          return <div key={item.id}> {item.id} <button onClick={() => removeAppointment(index)}>delete</button></div>;
        })}
      </div>
      <button onClick={addAppointment} >Add</button>
    </div>
  );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文