React 计数器不递增,而是加 1

React counter not incrementing, but adding 1 instead

我很困惑,我的 React 应用程序没有按预期增加计数器,而不是 1,2,3,4,5,而是 1,11,111,1111,11111 ... 我的理解是你不能做 this.state.count++ 因为这会改变 facebook 说不要做的状态,而他们说要做 this.state.count + 1。我对 React 很陌生,感谢你提供的任何帮助!谢谢!

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

class App extends Component {
  constructor(){
    super();
    this.state = {
      count: '0',
    }
    this.incrementCount = this.incrementCount.bind(this);
  }
  incrementCount() {
    this.setState({
      count: this.state.count + 1
    });
  }

  render() {
    return (
      <div className='app'>
        <div className='container'>
        <button onClick={this.incrementCount}>Click to increase bid: 
          {this.state.count}</button>
        </div>
     </div>
    );
  }
}
export default App;

您已将状态视为字符串,因为它在单引号中。在字符串上执行 + 连接。只需删除引号,它将是一个整数。