【问题标题】:re-render react component after i deleted from local storage?从本地存储中删除后重新渲染反应组件?
【发布时间】:2019-02-21 19:38:45
【问题描述】:

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

总结:我希望在激活方法 handleDelete() 后做出反应以重新呈现我的 displayValues

Github Link

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

【问题讨论】:

  • 当从localStorage删除时,也要从Display的状态中删除项目

标签: javascript reactjs local-storage


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你可以做喜欢接受的答案,你也可以有其他方式来做到这一点,如下所示:

    将您的handleDelete 事件移动到Parent 本身,您的状态data 就在那里。

     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>
        )
      }
    }
    

    Working Demo

    【讨论】:

    • 您的解决方案也有效。不能说哪个更好。
    • @ArnasDičkus 如果您除了删除之外不执行任何操作,则可以将 handleDelete 保留在 Parent 本身中,否则您可以按照 Harsha Venkatram 的回答
    猜你喜欢
    • 2020-12-27
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 2021-01-27
    相关资源
    最近更新 更多