php_modが非推奨になったのね

昔のバージョンのphp環境でphpバージョンを上げるという対応中。 apache + PHP8のインスタンスを構築してドキュメントルートまで到達を確認。 対応する人にドキュメントルートまで到達するからプロジェクトのファイルを置いてバージョンアップのエラーを確認してもらうため依頼しましたが HTTP 503 Service Unavailable のエラーが出ているとのこと。 HTMLファイルはアクセスできているからPHPの実行ができていないと思い調べると Apache HTTP サーバーで使用するために PHP に提供されている mod_php モジュールが非推奨になりました。 php-fpmをインストールして起動し、リクエストを流してphp-fmpでphpを実行するようにして解決。 しばらくPHPを触っていなかったのでモジュールが非推奨になったことに気づかずでした。

Reactを初めて触ってみました(tutorialその⑥)

Reactを初めて触ってみました(tutorialその⑥)

このページはReact公式ページのtutorialを体験したメモと初心者なりの解釈を記述したページになります。
前回は、オブジェクトのコピーで突然変異による更新(つまりは直接更新)と不変的な更新(元のオブジェクトをコピーして更新する)というマニュアルの説明をしていきました。またその続きになります。

機能コンポーネント
私たちはコンストラクタを削除しました。
実際、Reactは、レンダリングメソッドのみで構成されるSquareのようなコンポーネント型の関数コンポーネントと呼ばれるより単純な構文をサポートしています。
React.Componentを継承するクラスを定義するのではなく、単にpropsをとり、レンダリングすべきものを返す関数を書くだけです。

Squareクラス全体をこの関数で置き換えます。
function Square(props) {
  return (
    <button className="square" onClick={props.onClick}>
      {props.value}
    </button>
  );
}
関数コンポーネントと呼ばれるより単純な構文をサポートしている。


関数の書き方が省略できるということを説明しているようですね。
単純にpropsで記述できるようです。
onClick={() => this.props.onClick()
→onClick=this.props.onClick()

ターンを取る(Boardコンポーネントの変更)
私たちのゲームの明らかな欠陥は、Xだけがプレイできることです。それを修正しましょう。最初の移動をデフォルトで 'X'にしましょう。 Boardコンストラクタで開始状態を変更します。
class Board extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      squares: Array(9).fill(null),
      xIsNext: true,
    };
  }

上のconstructorでxIsNext: trueとなっているので下のhandleClick(i)が初回に実行された時に説明のデフォルトXになります。

私たちが移動するたびに、boolean値を反転して状態を保存することによってxIsNextをトグルします。 ボードのhandleClick関数を更新してxIsNextの値を反転させます。
  handleClick(i) {
    const squares = this.state.squares.slice();
    squares[i] = this.state.xIsNext ? 'X' : 'O';
    this.setState({
      squares: squares,
      xIsNext: !this.state.xIsNext,
    });
  }
現在の this.state.xlsNext を対象のSquares[i]に代入した後にthis.setStateで!this.state.xlsNextとすることで次が反転したplayerになるようにしています。


  render() {
    const status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');

    return (
      // the rest has not changed
ここは表示のplayerの表示を切り替える部分です。現在のthis.state.xIsNextの値に対してplayerを切り替え表示テキストを代入しています。

上で説明しているBoardコンポーネントが変更されると以下のようになっているはずです。
class Board extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      squares: Array(9).fill(null),
      xIsNext: true,
    };
  }

  handleClick(i) {
    const squares = this.state.squares.slice();
    squares[i] = this.state.xIsNext ? 'X' : 'O';
    this.setState({
      squares: squares,
      xIsNext: !this.state.xIsNext,
    });
  }

  renderSquare(i) {
    return (
      <Square
        value={this.state.squares[i]}
        onClick={() => this.handleClick(i)}
      />
    );
  }

  render() {
    const status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');

    return (
      <div>
        <div className="status">{status}</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>
    );
  }
}

次にゲームが勝ったときを見てみましょう。 
このヘルパー関数をファイルの最後に追加します。
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;
}
linesは勝つパターン、一直線に並ぶ四角の番号パターンですね。
linesのに一致するsquaresの番号を比較し値が一致していれば勝利者を返します。

掲示板のレンダリング機能でそれを呼び出すことで、誰かが勝利したかどうかをチェックし、誰かが勝利したときにステータス・テキストを「勝者:[X / O]」にすることができます。
ボードのレンダリングでのステータス宣言を次のコードに置き換えます。

  render() {
    const winner = calculateWinner(this.state.squares);
    let status;
    if (winner) {
      status = 'Winner: ' + winner;
    } else {
      status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
    }

    return (
      // the rest has not changed

誰かが既にゲームに勝った場合、または既に正方形が埋められている場合は、handleClickで判定してクリックを無視するように変更しています。

  handleClick(i) {
    const squares = this.state.squares.slice();
    if (calculateWinner(squares) || squares[i]) {
      return;
    }
    squares[i] = this.state.xIsNext ? 'X' : 'O';
    this.setState({
      squares: squares,
      xIsNext: !this.state.xIsNext,
    });
  }

ちょっと効率が悪い気がしますが、直線ラインが埋まっているかをクリックの度に判定してクリック後の処理を無視するか、クリックした四角がすでにどちらかのplayerが埋めていないかを判定して無視する処理を加えています。

ここまでで○☓ゲームはできました。

コメント

このブログの人気の投稿

nginxで画像が表示されない。。

AWSのS3バケットをマウントするs3fsでマウントが外れた件

CakePHP3でHTTPSにする