Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodejs 16.13.0
16.13.0

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

前PRから引き継ぎ

ookura-mf 1 hour ago
nodejsが入ってるとnodejsなんて知らん!ってerror出たので修正したが、実はそのままでいけるのかわかっていない(nodenvのrepositoryでgrepした感じもヒットしなかった)

Owner
@Y4suyuki Y4suyuki 26 minutes ago
もしかしたら僕の用意したのが間違っていたかもです 😅
nodeenv 使ったことなくて、今も使っていません
nodeのバージョン管理だとnvmの方が主流な気がします(スターの数とかブログとかで取り上げられる頻度)
僕はasdfを使って .nvmrc を読むようにしてます

https://github.com/ekalinin/nodeenv
https://github.com/nvm-sh/nvm
https://github.com/asdf-vm/asdf

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvmの方が主流なんですね 👀
anyenvな気持ちですぐ使っちゃった 🙈

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

まあnodeenvもnode.jsの公式サイトに紹介されているしバージョンマネージャでもDockerでもnodeのバージョンさえあっていれば良いのでそこは個人の好みでも良いと思います(余程マイナーなものでなければ)

1 change: 1 addition & 0 deletions tic-tac-toe-app/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

21 changes: 21 additions & 0 deletions tic-tac-toe-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tic-tac-toe-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"prettier": "2.5.0"

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Y4suyuki Y4suyuki 1 hour ago
👍

Owner
@Y4suyuki Y4suyuki 1 hour ago
prettierのconfigってどこにありますか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Y4suyuki
普段これがいいってものも持ってないので全部defaultで使ってみようと思い特に用意してなかったです 🙈
が、とりあえず空ファイルだけでもおいときます

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

空ファイルを置くことが重要です!
prettierを使う場合Repoで設定を共有することが目的なので個人がプロジェクトのディレクトリに勝手においたり、個人のグローバルの設定を読みにいくのを防ぎます

あと設定についてはそもそもPrettierは(ESlintも個人的には)頑張って設定するものではないです
詳しくは
https://prettier.io/docs/en/option-philosophy.html

}
}
145 changes: 117 additions & 28 deletions tic-tac-toe-app/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import reportWebVitals from './reportWebVitals';
import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import reportWebVitals from "./reportWebVitals";

class Square extends React.Component {
render() {
return (
<button className="square">
{/* TODO */}
</button>
);
}
type SquareValue = "X" | "O" | null;
type SquareProps = {
value: SquareValue;
onClick: () => void;
};
function Square(props: SquareProps) {
return (
<button className="square" onClick={props.onClick}>
{props.value}
</button>
);
}

class Board extends React.Component {
renderSquare(_: number) {
return <Square />;
type BoardProps = {
squares: SquareValue[];
onClick: (i: number) => void;
};
class Board extends React.Component<BoardProps> {
renderSquare(i: number) {
return (
<Square
value={this.props.squares[i]}
onClick={() => this.props.onClick(i)}
/>
);
}

render() {
const status = 'Next player: X';

return (
<div>
<div className="status">{status}</div>
<div className="board-row">
{this.renderSquare(0)}
{this.renderSquare(1)}
Expand All @@ -43,30 +52,110 @@ class Board extends React.Component {
);
}
}

class Game extends React.Component {
type GameHistory = {
squares: SquareValue[];
};
type GameProps = {};

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ookura-mf ookura-mf 1 hour ago
propsを取らない場合は空であることを明示しておいた方が良いのかな、というのとコンパイラがエラー吐くので定義してみたけどあっているかわからない

Owner
@Y4suyuki Y4suyuki 23 minutes ago
あっているかわからない

あっているとは何を指していますか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あっているとは何を指していますか?

一般的な書き方(デファクト)なのかわからない、という意図でした!

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一般的な書き方(デファクト)なのかわからない

なるほど、それで言うと一般的な書き方(デファクト)をそこまで意識する必要はないと思います
railsのconvention over configuration的な考え方はReact / Typescriptには当てはまらないと思います
conventionもあるにはあると思いますがまず、動くこと、一般的なProgrammingのgood practice (DRY, KISS, YAGNIなど)に当てはまっていることを考慮した上でそれでも揺らぎがある場合にチームでスタイルを統一すれば良いと思います
ここで言うとclass componentがpropsを取らないとしてもgenericsの型は設定しないといけないので迷うとしたら、名前をつけて定義するかgenericsのところにliteralで表記するかどちらかと思いますが、どちらでもコードとして大差ないのでどっちでもいいのではと思います(こう言うところの書き方にこだわって時間使う方が無駄だと思います)

type GameState = {
history: GameHistory[];
stepNumber: number;
xIsNext: boolean;
};
class Game extends React.Component<GameProps, GameState> {
constructor(props: GameProps) {
super(props);
this.state = {
history: [
{
squares: Array(9).fill(null),
},
],
stepNumber: 0,
xIsNext: true,
};
}
handleClick(i: number) {
const history = this.state.history.slice(0, this.state.stepNumber + 1);
const current = history[this.state.stepNumber];
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: number) {
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);
let status;
if (winner) {
status = "Winner: " + winner;
} else {
status = "Next player: " + (this.state.xIsNext ? "X" : "O");
}
const moves = history.map((_, move) => {
const desc = move ? "Go to move #" + move : "Go to game start";
return (
<li key={move}>
<button onClick={() => this.jumpTo(move)}>{desc}</button>
</li>
);
});
return (
<div className="game">
<div className="game-board">
<Board />
<Board
squares={current.squares}
onClick={(i) => this.handleClick(i)}
/>
</div>
<div className="game-info">
<div>{/* status */}</div>
<ol>{/* TODO */}</ol>
<div>{status}</div>
<ol>{moves}</ol>
</div>
</div>
);
}
}

// ========================================
function calculateWinner(squares: Array<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 (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;
}

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

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

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
Expand Down