LoginSignup
0

More than 3 years have passed since last update.

React tutorialをTypeScript・Functional Component・React hooksでやってみる

Last updated at Posted at 2020-12-20

VISITS Technologies Advent Calendar 2020 21日目は@woods0918が担当します。

私がプロダクトマネージャーを務めているideagramというサービスのフロントエンドのReact環境が割をモダンであると聞いたので、フロントエンジニアとの会話をスムーズにするために、ReactチュートリアルをTypeScript・Functional Component・React hooksを使ってやってみようと思います。

コードはこちら

書き換え前のコード

まずは、Reactチュートリアルにおける完成後のコードとなります。

index.jsx
function Square(props) {
  return (
    <button className="square" onClick={props.onClick}>
      {props.value}
    </button>
  );
}

class Board extends React.Component {
  renderSquare(i) {
    return (
      <Square
        value={this.props.squares[i]}
        onClick={() => this.props.onClick(i)}
      />
    );
  }

  render() {
    return (
      <div>
        <div className="board-row">
          {this.renderSquare(0)}
          {this.renderSquare(1)}
          {this.renderSquare(2)}
        </div>
        <div className="board-row">
          {this.renderSquare(3)}
          {this.renderSquare(4)}
          {this.renderSquare(5)}
        </div>
        <div className="board-row">
          {this.renderSquare(6)}
          {this.renderSquare(7)}
          {this.renderSquare(8)}
        </div>
      </div>
    );
  }
}

class Game extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      history: [
        {
          squares: Array(9).fill(null)
        }
      ],
      stepNumber: 0,
      xIsNext: true
    };
  }

  handleClick(i) {
    const history = this.state.history.slice(0, this.state.stepNumber + 1);
    const current = history[history.length - 1];
    const squares = current.squares.slice();
    if (calculateWinner(squares) || squares[i]) {
      return;
    }
    squares[i] = this.state.xIsNext ? "X" : "O";
    this.setState({
      history: history.concat([
        {
          squares: squares
        }
      ]),
      stepNumber: history.length,
      xIsNext: !this.state.xIsNext
    });
  }

  jumpTo(step) {
    this.setState({
      stepNumber: step,
      xIsNext: (step % 2) === 0
    });
  }

  render() {
    const history = this.state.history;
    const current = history[this.state.stepNumber];
    const winner = calculateWinner(current.squares);

    const moves = history.map((step, move) => {
      const desc = move ?
        'Go to move #' + move :
        'Go to game start';
      return (
        <li key={move}>
          <button onClick={() => this.jumpTo(move)}>{desc}</button>
        </li>
      );
    });

    let status;
    if (winner) {
      status = "Winner: " + winner;
    } else {
      status = "Next player: " + (this.state.xIsNext ? "X" : "O");
    }

    return (
      <div className="game">
        <div className="game-board">
          <Board
            squares={current.squares}
            onClick={i => this.handleClick(i)}
          />
        </div>
        <div className="game-info">
          <div>{status}</div>
          <ol>{moves}</ol>
        </div>
      </div>
    );
  }
}

// ========================================

ReactDOM.render(<Game />, document.getElementById("root"));

function calculateWinner(squares) {
  const lines = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6]
  ];
  for (let i = 0; i < lines.length; i++) {
    const [a, b, c] = lines[i];
    if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
      return squares[a];
    }
  }
  return null;
}

書き換え後のコード

次に、TypeScript・Functional Component・React hooksで書き換えた後のコードになります。

index.tsx
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import './index.css';

// ========================================

type SquareValue = 'X' | 'O' | null
type SquareHandleClick = () => void
type SquareValues = SquareValue[]
type HandleClick = (i: number) => void
type historySquareValues = {squares: SquareValues}[]
type historyStates = {
    history: historySquareValues,
    stepNumber: number,
    xIsNext: boolean
}

type SquareProps = {
    value: SquareValue
    onClick: SquareHandleClick
}

type BoardProps = {
    squares: SquareValues,
    onClick: HandleClick
}
type GameProps = {}

// ========================================

const Square: React.FC<SquareProps> = props => {
    return (
    <button className="square" onClick={()=>props.onClick()}>
        { props.value }
    </button>   
    );
}

const Board: React.FC<BoardProps> = props => {

    const renderSquare = (i: number) => {
        return (
        <Square 
            value={props.squares[i]} 
            onClick={()=>props.onClick(i)}
        />
        );
    }

    return (
        <div>
            <div className="board-row">
                {renderSquare(0)}
                {renderSquare(1)}
                {renderSquare(2)}
            </div>
            <div className="board-row">
                {renderSquare(3)}
                {renderSquare(4)}
                {renderSquare(5)}
            </div>
            <div className="board-row">
                {renderSquare(6)}
                {renderSquare(7)}
                {renderSquare(8)}
            </div>
        </div>
    );
}

const Game: React.FC<GameProps> = props => {
    const [state, setState] = useState<historyStates>({
        history: [{squares: Array(9).fill(null)}],
        stepNumber: 0,
        xIsNext: true
    })

    const history = state.history;
    const current = history[state.stepNumber];
    const winner = calculateWinner(current.squares);
    const moves = history.map((step, move) => {
        const desc = move ?
            'Go to move #' + move :
            'Go to game start';
        return (
            <li key={move}>
                <button onClick={() => {
                    setState({
                        history: state.history,
                        stepNumber: move, 
                        xIsNext: (move % 2) === 0
                    })
                }}>{desc}</button>
            </li>
        );
    });

    let status: string;
    if (winner) {
        status = 'Winner: ' + winner;
    } else {
        status = 'Next player: ' + (state.xIsNext ? 'X': 'O');
    }

    return (
        <div className="game">
            <div className="game-board">
                <Board
                    squares={current.squares}
                    onClick={(i: number) => {
                        const history = state.history;
                        const current = history[history.length - 1];
                        const squares = current.squares.slice();
                        if (calculateWinner(squares) || squares[i]) {
                            return;
                        }
                        squares[i] = state.xIsNext ? 'X': 'O';
                        setState({
                            history: history.concat([{
                                squares: squares
                            }]),
                            stepNumber: history.length,
                            xIsNext: !state.xIsNext
                        });
                    }}
                />
            </div>
            <div className="game-info">
                <div>{status}</div>
                <ol>{ moves }</ol>
            </div>
        </div>
    );
}

// ========================================
const calculateWinner = (squares: SquareValues): SquareValue => {
    const lines = [
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8],
        [0, 3, 6],
        [1, 4, 7],
        [2, 5, 8],
        [0, 4, 8],
        [2, 4, 6],
      ];

      for (const line of lines) {
          const [a, b, c] = line;
          if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
              return squares[a];
            }
      }

      return null
}


// ========================================

ReactDOM.render(
   <Game />,
   document.getElementById('root')
);

書き換えをしてみて

何よりTypeScriptで型を定義できることによる可読性と安心感の向上を感じました。
やはり、コメントがなくとも読むだけである程度何をするか分かることのメリットは大きいですね。

また、Functional Componentを利用することで、constructor・super・thisなど書かなくて良くなり、非常にすっきりとストレスなくコードを書くことができたと感じます。また、hooksを利用することで、状態は扱いやすくなったのも書き換えの利点だと感じました。

最後に

タイムオーバーで、書き換えにあたっての細かい説明を入れることが出来ませんでしたが、機を見て徐々に説明を書き足していきたいと思います。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0