简体   繁体   English

反应不更新DOM

[英]react does not update DOM

import React, { Component } from "react";
import ReactDOM from "react-dom";
import "./index.css";


class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
        text: "",
        listItem: []
    }
    this.onChangeInput = this.onChangeInput.bind(this);
    this.addToList = this.addToList.bind(this);
    this.keyPress = this.keyPress.bind(this);
  }
  onChangeInput(event) {
    this.setState({
        text: event.target.value
    });
  }
  addToList () {
      let list = this.state.listItem;
      list.push(this.state.text);
      this.setState({
          text: ""
      });
      this.setState({
        listItem: list
      });
  }
  deleteItem(event) {
    console.log(event.target.remove());
  }
  keyPress (e) {
      if (e.key === "Enter") {
          this.addToList()
      }
  }
  render() {
    const listItem = this.state.listItem;
    const list = listItem.map((val, i) => 

        <li key={i.toString()} onClick={this.deleteItem}>
            {val}
        </li>


    );
    console.log(list);
    return (
      <div className="container">
          <Input onChange={this.onChangeInput} value={this.state.text}
              keyPress={this.keyPress}
          />
          <Button addToList={this.addToList}/>
          <ul>
              {list}
          </ul>
      </div>
    );
  }
}
class Input extends Component {

  render() {
    return <input type="text" className="input" onChange={this.props.onChange}
     onKeyPress={this.props.keyPress}
     value={this.props.value}/>;

  }
}
class Button extends Component {
  render() {
    return (
      <button className="button" onClick={this.props.addToList}>
        Add To List
      </button>
    );
  }
}


ReactDOM.render(<App />, document.getElementById("root"));

I'm very confused and couldn't find solution any where.我很困惑,无法在任何地方找到解决方案。 I'm new to react.我是新来的反应。 when I delete the list items, I delete them from DOM but is in state and I didn't delete it from state.当我删除列表项时,我将它们从 DOM 中删除但处于 state 中并且我没有从 state 中删除它。 I put console.log(list) in render method and on every key press logs list in console my question is why DOM does not re-render lists and output those where deleted from DOM and not from state?我将console.log(list)放在渲染方法中,并且在控制台中的每个按键日志列表中,我的问题是为什么 DOM 不重新渲染列表并输出那些从 DOM 中删除而不是从状态中删除的列表? and why it works for new list items and ignore those that deleted from DOM ?以及为什么它适用于新的列表项而忽略那些从 DOM 中删除的项?

react dosent pickup the update in the way you are doing it以您正在执行的方式做出反应来获取更新

deleteItem(event) {
   console.log(event.target.remove());
}

although the item will be removed , but react dosent have any clue that happend, to notify react that the items has changed and it need to re-render, you need to call setState , then react calls the render method again,虽然item会被移除,但是react没有任何线索发生,要通知react item已经改变需要重新渲染,你需要调用setState,然后react再次调用render方法,


deleteItem(e) {
  const list=  this.state.listItem;
  list.pop() // remove the last element
  this.setState({
    list: list
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM