【问题标题】:How to delete selected element react如何删除选定的元素反应
【发布时间】:2019-07-15 12:42:11
【问题描述】:

我有两个组件 Display.jsx 和 DisplayList.jsx。组件协同工作以显示来自本地存储的值。问题在于 DisplayList.JSX handleDelete() 方法循环。

我不明白为什么它会删除第一个元素而不是选定的元素?以及如何解决这个问题?

Github

显示.jsx

import {DisplayList} from './DisplayList';


class Display extends Component {
  constructor(props){
    let data = JSON.parse(localStorage.getItem('data'));
    super(props)
    this.state = {
      data: data,
  }

  // Methods
  this.displayValues = this.displayValues.bind(this);
  }

  displayValues(){

   return this.state.data.map((data1, index) =>
    <DisplayList
      key = {index}
      email = {data1.email}
      password = {data1.password}
      updateList = {this.updateList}
       /> 
    )
  }
  // This is the method that will be called from the child component.
  updateList = (data) => {
    this.setState({
      data
    });
  }
  render() {
    return (
      <ul className="list-group">
        {this.displayValues()}
      </ul>
    )
  }
}

export default Display;

DisplayList.jsx

import React, { Component } from 'react'
import {Button, Modal, Form} from 'react-bootstrap';


export class DisplayList extends Component {

    constructor(props){
        super(props)
        this.state = {
            email: '',
            password: '',
            show: false,
        };

        // Methods
        this.handleDelete = this.handleDelete.bind(this);
        this.onChange = this.onChange.bind(this);
    }

    onChange(event){
        this.setState({
            [event.target.name]: event.target.value
        })
    };   
    handleDelete(){
        const data = JSON.parse(localStorage.getItem('data'));
        for (let index = 0; index < data.length; index++) {
            if(this.props.email === data[index].email &&
                this.props.password === data[index].password){
                  console.log(this.props.email);
                  console.log(data[index]);
                data.splice(data[index], 1);
            }
            console.log('loop count');
        }
        localStorage.setItem('data', JSON.stringify(data));
        this.props.updateList(data);
    }


  render() {
    return (
    <div className = "mt-4">
        <li className="list-group-item text-justify">
            Email: {this.props.email} 
            <br /> 
            Password: {this.props.password}
            <br /> 
            <Button onClick = {this.handleShow} variant = "info mr-4 mt-1">Edit</Button>
            <Button onClick = {this.handleDelete} variant = "danger mt-1">Delete</Button>
        </li>
    </div>
    )
  }
}

【问题讨论】:

  • 而不是 for 循环使用 if 为什么不使用过滤器并返回新数组?array.prototype.filter... 类似这样的: const newData = data.filter(credential => data. email !== this.props.email && data.password !== this.props.password) 数组过滤器链接 --> developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

标签: javascript reactjs local-storage


【解决方案1】:

代替:

data.splice(data[index], 1);

尝试重写整个数组:

data = [
    ...data.slice(0, index),
    ...data.slice(index + 1)
];

【讨论】:

  • 这会在现有代码中引发错误。因为数据是const,不是吗?
  • 它工作我被困了一个多小时。谢谢。您还可以帮助编辑本地存储吗?如果我想编辑特定的值,我应该怎么写? github.com/ArnasDickus/enigma-assignment/blob/master/src/…
  • @SubhenduKundu 我将数据类型更改为让。没有必要保持它为 const。
【解决方案2】:
handleDelete(){
  const data = JSON.parse(localStorage.getItem('data'));
  const filteredData = data.filter(
      x => !(x.email === this.props.email && x.password === this.props.password)
   );
  localStorage.setItem('data', JSON.stringify(filteredData));
  this.props.updateList(filteredData);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 2017-05-05
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    相关资源
    最近更新 更多