【问题标题】:cant remove item from array by index无法按索引从数组中删除项目
【发布时间】:2021-12-06 22:10:27
【问题描述】:

我似乎正确地传递了一切。

  • 当我尝试从数组中删除项目时,它会删除除我要删除的项目之外的所有其他项目。也祭坛的项目。例如,如果项目是字符串 ss。
  • 如果我从数组中删除最后一个 2 个字符的项目,例如 77。它会将数组中每个 2 个字符长的项目更改为 7。并删除所有其他项目
  • 如果我从数组中删除第一项,它会清除整个数组

我需要做什么

从数组中删除与 var itemIndex 匹配的项目


母公司

const Pagethree = () => {
const[items,setItem] = useState([]);
return(
        <ul>
            {
            items.map((items, i) =>
                <ListItem index={i} item={items} setItem={setItem}/>)
            }
        </ul>
)

儿童补偿

import React, {useState} from "react";

const ListItem = (props) =>{
const {item, setItem, index} = props;

const removeItem = e =>{
    var array = [...item];
    var indexItem = index;
    if (indexItem !== -1){
        array.splice(indexItem, 1);
        setItem(array);
    }
    console.log(array);
}

return(
    <div>
        <ul>
            {
                <div class="flex">
                    {item}
                    <button onClick={removeItem}>delete</button>
                </div>
            }
        </ul>
    </div>
)
};

export default ListItem;

【问题讨论】:

  • 您有要过滤的 id 吗?例如 array.filter(item => item.id !== id) 并且你在 onClick={()=> removeItem(id)} 中传递 id 并通过 removeItem 的道具传递
  • 理想情况下,您的父组件应该管理所有项目的状态,并且只为道具中的按钮传递一个处理函数,然后使用该已删除项目的 id 更新状态。

标签: javascript arrays reactjs slice


【解决方案1】:

您的父组件应该管理状态。您应该从父组件向下传递给子组件的是它需要呈现的数据,以及删除按钮的处理程序。

const { useState } = React;

// Passing a value, and index, and the handler
function ListItem({ value, index, updateState }) {
  
  // When `handleDelete` is called, call
  // the `updateState` with the item index
  // as an argument
  function handleDelete() {
    updateState(index);
  }
  
  // The button calls the local function when
  // it is clicked
  return (
    <li>
      {value}
      &nbsp;
      <button onClick={handleDelete}>Delete</button>
    </li>
  );
}

function Example({ data }) {

  const [ items, setItems ] = useState(data);

  // `filter` out the items that don't have
  // the deleted item's index, and update state
  function updateState(index) {
   const updated = items.filter((_, i) => i !== index);
   setItems(updated);
  }

  // `map` over the data making sure
  // that `updateState` is passed down in the props
  return (
    <ul>
      {items.map((el, i) => {
        return (
          <ListItem
            key={i}
            value={el}
            index={i}
            updateState={updateState}
          />
        )
      })}
    </ul>
  );
};

const data = [1, 2, 3, 4 ];

ReactDOM.render(
  <Example data={data} />,
  document.getElementById('react')
);
ul { list-style-type: none;  padding: 0; margin: 0; }
li { margin-bottom: 1em; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script>
<div id="react"></div>

【讨论】:

  • 这很棒而且很有帮助。但我很好奇为什么当我在 removeItem 函数中注释掉所有内容并且只有 `var array = [...item];控制台.log(数组);' .它只是将我要删除的项目记录为项目。例如 bob 正在记录为 ['b','o', 'b']。相反,它应该记录整个名称列表,而不仅仅是分解的一个名称。
  • 测试一下,你会看到的。 const item = 'Bob'; console.log([...item]); 你正在将一个字符串拆分成一个数组。
  • 我搞定了。我不得不做一些事情来映射单个项目而不是发送整个数组。
猜你喜欢
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 2018-11-04
  • 2021-03-30
  • 2016-04-22
  • 1970-01-01
  • 2021-06-18
  • 2021-04-20
相关资源
最近更新 更多