简体   繁体   English

代码运行缓慢时如何使用console.log调试代码

[英]how to debug code with console.log when the code runs slow

I'm practicing react, and just now I'm about to make a to-do list.我正在练习react,现在我要列一个to-do list。 While i was working.在我工作的时候。 I wanted to test my code with console.log to see input values are passing correctly to state.我想用 console.log 测试我的代码以查看输入值是否正确传递到状态。 However for awhile I was confused to see how console.log would always output the previous state.然而,有一段时间我很困惑,看到 console.log 总是输出以前的状态。 Until later, i just embeded <p>{this.state.myArray}</p> and it shows it is working correctly.直到后来,我才嵌入<p>{this.state.myArray}</p>并显示它工作正常。 I presume this.setState({ myArray: this.state.message });我认为this.setState({ myArray: this.state.message }); is still finishing executing while console.log already executed.在console.log 已经执行时仍在完成执行。

I'm pretty sure im using console.log the wrong way to test code.我很确定我使用 console.log 测试代码的方式是错误的。 New programmer here.新程序员来了。

class ToDoInput extends React.Component {
  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.state = { message: "", myArray: "" };
  }

  handleChange(e) {
    this.setState({ message: e.target.value });
    console.log("handleChange: " + this.state.message); //testing
  }

  handleSubmit(e) {
    this.setState({ myArray: this.state.message });
    console.log("handleSubmit: " + this.state.myArray); //testing
  }

  render() {
    return (
      <form>
        <input
          type="text"
          value={this.state.content}
          onChange={this.handleChange}
        />
        <input type="button" value="submit" onClick={this.handleSubmit} />
        <p>{this.state.myArray}</p>
      </form>
    );
  }
}

codeSandBox link 代码沙盒链接

The problem you're running into is that setState is asynchronous and that it does not set the state immediately but after a short delay.您遇到的问题是setState是异步的,它不会立即设置状态,而是在短暂延迟后设置状态。 There are several reasons that React does this, but one of them is that it allows React to group multiple state changes and then rerender your component a single time, instead of re-rendering every time setState is called. React 这样做有几个原因,但其中之一是它允许 React 将多个状态更改分组,然后一次重新渲染您的组件,而不是每次调用 setState 时重新渲染。

If you want to use the state after setting the state, you can use the second argument of setState like this:如果你想在设置状态后使用状态,你可以像这样使用setState的第二个参数:

  handleSubmit(e) {
    this.setState({ myArray: this.state.message }, () => {
      console.log("handleSubmit: " + this.state.myArray);
    });
  }

You can read more about setState is asynchronous here. 您可以在此处阅读有关 setState 是异步的更多信息。 Although that Github issue is probably more in-depth than you're looking for.尽管 Github 问题可能比您正在寻找的更深入。

In your example above, it might be worth putting the console.log(this.state) within your render function, as that should always have the most up-to-date state.在上面的示例中,将console.log(this.state)放在render函数中可能是值得的,因为它应该始终具有最新状态。

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

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