@testtoster

Как сделать увеличение числа?

Подскажите, как сделать так, чтобы по щелчку на кнопку count к числу прибавлялась единица?
import React from 'react';
import './App.css';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      text: '',
      count: 0,
    }
  }

  showText = () => {
    console.log('button work');
    this.setState({ text: 'button work' });
  }

  showCount = () => {
    this.setState({ count: '11' });
  }

  render() {
    return (
      <div>
        <button onClick={this.showText}>Push1</button>
        <p>{this.state.text}</p>
        <button onClick={this.showCount}>Count</button>
        <p>{this.state.count}</p>
      </div>
    )
  }
}

export default App;
  • Вопрос задан
  • 487 просмотров
Решения вопроса 1
GreyCrew
@GreyCrew
Full-stack developer
import React from 'react';
import './App.css';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      text: '',
      count: 0,
    }
  }

  showText = () => {
    console.log('button work');
    this.setState({ text: 'button work' });
  }

  showCount = () => {
    this.setState({ count: this.state.count + 1});
  }

  render() {
    return (
      <div>
        <button onClick={this.showText}>Push1</button>
        <p>{this.state.text}</p>
        <button onClick={this.showCount}>Count</button>
        <p>{this.state.count}</p>
      </div>
    )
  }
}

export default App;

( -__ -)
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы