当前位置: 代码迷 >> JavaScript >> 从本地存储删除后重新渲染React组件?
  详细解决方案

从本地存储删除后重新渲染React组件?

热度:116   发布时间:2023-06-05 09:33:41.0

我有2个组成部分。 显示和DisplayList。 组件协同工作以显示本地存储中的值。 一切正常。 但是当我激活handleDelete方法时。 值将从本地存储中删除,但react不会重新呈现列表。

摘要:我想在激活方法handleDelete()之后做出反应以重新渲染displayValues

显示.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}
       /> 
    )

  }
  render() {
    return (
      <ul className="list-group">
        {this.displayValues()}
      </ul>
    )

  }
}

DisplayList.JSX

import {Button} from 'react-bootstrap';


export class DisplayList extends Component {
    constructor(props){
        super(props)


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


    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){
                data.splice(data[index], 1);
            }
        }
        localStorage.setItem('data', JSON.stringify(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 variant = "info mr-4 mt-1">Edit</Button>
            <Button onClick = {this.handleDelete} variant = "danger mt-1">Delete</Button>
        </li>  
    </div>
    )
  }
}

实现此目标的一种可能方法是通过将回调函数作为propDisplay.JSX发送到DisplayList.JSX 然后从handleDelete触发对父对象的回调并在其中设置状态。 示例代码如下。

Display.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>
    )

  }
}

DisplayList.jsx

import {Button} from 'react-bootstrap';


export class DisplayList extends Component {
    constructor(props){
        super(props)


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


    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){
                data.splice(data[index], 1);
            }
        }
        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 variant = "info mr-4 mt-1">Edit</Button>
            <Button onClick = {this.handleDelete} variant = "danger mt-1">Delete</Button>
        </li>  
    </div>
    )
  }
}

您可以像接受的答案一样做,也可以采用其他方式来做到这一点,如下所示:

将您的handleDelete事件handleDelete状态data所在的Parent本身中。

 class Display extends Component {
    constructor(props){
      let data = JSON.parse(localStorage.getItem('data'));
      super(props)
      this.state = {
        data: data || [{email: 'j', password: "dsv" }],
    }
  }


    handleDelete(email, password){
        const data = JSON.parse(localStorage.getItem('data'));
        data = data ? data : this.state.data
        for (let index = 0; index < data.length; index++) {
            if(email === data[index].email &&
                password === data[index].password){
                data.splice(data[index], 1);
            }
        }
        localStorage.setItem('data', JSON.stringify(data));
        this.setState({data});
    }

  render() {
    console.log(this.state)
    debugger
    return (
      <ul className="list-group">
        {this.state.data.map((data1, index) =>
          <DisplayList
            key = {index}
            email = {data1.email}
            password = {data1.password}
            handleDelete = {() => this.handleDelete(data1.email,data1.password )}
            /> 
          )}
      </ul>
    )

  }
}

并在DisplayList

    export default class DisplayList extends Component {
    constructor(props){
        super(props)
    }


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

  相关解决方案