简体   繁体   English

为什么在 state 更改后 React 不重新渲染页面?

[英]Why isn't React re-rendering the page after the state is changed?

so I was working on a basic Todo app using React.js and I was wondering why the todo component does not automatically re-render once the state changed (the state contains the list of todos- so adding a new todo would update this array)?所以我正在使用 React.js 开发一个基本的 Todo 应用程序,我想知道为什么一旦 state 更改后 todo 组件不会自动重新渲染(state 包含待办事项列表 - 所以添加新的 todo 会更新这个数组) ? It is supposed to re-render the Header and the Todo component of the page with the updated array of todos passed in as props.它应该重新渲染 Header 和页面的 Todo 组件,其中更新的 todo 数组作为 props 传入。 Here is my code:这是我的代码:

import React from 'react';
import './App.css';

class Header extends React.Component {
  render() {
    let numTodos = this.props.todos.length;
    return <h1>{`You have ${numTodos} todos`}</h1>
  }
}

class Todos extends React.Component {
  render() {
    return (
    <ul>
      {
    this.props.todos.map((todo, index) => {
      return (<Todo index={index} todo={todo} />)
    })
    }
    </ul>
    )
  }
}

class Todo extends React.Component {
  render() {
    return <li key={this.props.index}>{this.props.todo}</li>
  }
}

class Form extends React.Component {
  constructor(props) {
    super(props);
    this.addnewTodo = this.addnewTodo.bind(this);
  }

  addnewTodo = () => {
    let inputBox = document.getElementById("input-box");
    if (inputBox.value === '') {
      return;
    }
    this.props.handleAdd(inputBox.value);
  }

  render() {
    return (
      <div>
        <input id="input-box" type="text"></input>
        <button type="submit" onClick={this.addnewTodo}>Add</button>
      </div>
    )
  }
}

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { todos: ['task 1', 'task 2', 'task 3']}
    this.handleNewTodo = this.handleNewTodo.bind(this);
  }

  handleNewTodo(todo) {
    let tempList = this.state.todos;
    tempList.push(todo);
    this.setState = { todos: tempList };
  }

  render() {
    return (
      <div>
      <Header todos={this.state.todos} />
      <Todos todos={this.state.todos} /> 
      <Form todos={this.state.todos} handleAdd={this.handleNewTodo} />
      </div>
      )
  }
}

You are not updating the state correctly.您没有正确更新 state。

You need to make a copy of the this.state.todos , add the new todo in the copied array and then call this.setState function您需要复制this.state.todos ,在复制的数组中添加新的 todo 然后调用this.setState

handleNewTodo(todo) {
    let tempList = [...this.state.todos];
    tempList.push(todo);
    this.setState({ todos: tempList });
}

Notice that this.setState is a function注意this.setState是一个 function

You're updating state incorrectly,您正在错误地更新 state,

handleNewTodo(todo) {
    let tempList = [...this.state.todos];
    tempList.push(todo);
    this.setState({ todos: tempList });
  }

This is the correct syntax.这是正确的语法。

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

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