본문 바로가기

프로그램 강좌

[react.js]간단한 카운터 만들기


아주아주 간단한 카운터 만들기


App.js

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

class App extends Component {
  render() {
    return (
      <div>
        <Counter />
      </div>
    );
  }
}

export default App;


Counter.js

import React, { Component } from 'react';

class Counter extends Component {
  state = {
    number: 0
  };
  handleIncrease = () => {
    this.setState({
      number: this.state.number + 1
    });
  };
  handleDecrease = () => {
    this.setState({
      number: this.state.number - 1
    });
  };
  render() {
    const { number } = this.state;
    const { handleIncrease, handleDecrease } = this;
    return (
      <div>
        <h1>{number}</h1>
        <button onClick={handleIncrease}>+</button>
        <button onClick={handleDecrease}>-</button>
      </div>
    );
  }
}

export default Counter;

728x90