简体   繁体   English

React 待办事项列表显示待办事项 onChange,而不是 onSubmit

[英]React todo list displays todo onChange, not onSubmit

I'm a bit new to React, and with all new programming endeavors I am building a todo app.我对 React 有点陌生,通过所有新的编程工作,我正在构建一个待办事项应用程序。 Everything seems to be working correctly except for one issue: When I enter a todo into the input field and click "submit", the todo is pushed into my array, however it doesn't immediately display.除了一个问题外,一切似乎都正常工作:当我在输入字段中输入一个待办事项并单击“提交”时,该待办事项会被推入我的数组中,但它不会立即显示。 It is only when I change the text inside the input that the todo is displayed.只有当我更改输入中的文本时才会显示待办事项。 I'm guessing this has something to do with the rendering happening on the handleChange function and not the handleSubmit function.我猜这与在 handleChange 函数而不是 handleSubmit 函数上发生的渲染有关。 Any help would be greatly appreciated.任何帮助将不胜感激。

Here is my AddTodo component这是我的 AddTodo 组件

import React, { Component } from 'react';
import App from "./App"
import List from "./List"


class AddTodo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: '',
      array: []
    };

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
     this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    event.preventDefault();
    var array = this.state.array
    array.push(this.state.value);
    console.log(array)
  }

  render() {
    return (
      <div>
        <form>
          <label>
            Name:
            <input type="text" value={this.state.value} onChange={this.handleChange} />
          </label>
          <input onClick={this.handleSubmit} type="submit" value="Submit" />
       </form>
       <List array={this.state.array}/>
       </div>
     );
   }
 }

And my List component还有我的 List 组件

import React, { Component } from 'react';

class List extends Component{
  render(){
    return(
      <div>
      {
        this.props.array.map(function(item, index){
          return <li key={index}>{item}</li>
        })
      }
      </div>
    )
  }
}

export default List;

By default, invoking setState() calls the render() function.默认情况下,调用 setState() 会调用 render() 函数。 More info here: ReactJS - Does render get called any time "setState" is called?这里有更多信息: ReactJS - 每次调用“setState”时都会调用渲染吗?

React renders an individual component whenever its props or state change.每当propsstate发生变化时,React 都会渲染一个单独的组件。 In order to make a change in the state, with a class component it's mandatory to use the this.setState() method, which among other things makes sure to call the render() when it's necessary.为了改变状态,对于类组件,必须使用this.setState()方法,该方法确保在必要时调用render()

Your handleSubmit() method is changing the array directly , which is forbidden (it's only allowed in the constructor in order to set the initial state)您的handleSubmit()方法正在直接更改数组,这是禁止的(仅在构造函数中允许以设置初始状态)

If you use setState() it should work.如果您使用setState()它应该可以工作。

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

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