Skip to content

hooksに置き換え#2

Open
ookura-mf wants to merge 19 commits into
training0from
training1
Open

hooksに置き換え#2
ookura-mf wants to merge 19 commits into
training0from
training1

Conversation

@ookura-mf

@ookura-mf ookura-mf commented Dec 6, 2021

Copy link
Copy Markdown
Owner
  • componentを最小単位っぽい感じに分割
  • 複数のstateを扱っていた箇所をuseReducerにまとめる
  • ロジックっぽいものをusecase層にお引っ越し
  • eslintを雑に導入

Comment thread tic-tac-toe-app/src/components/square/index.tsx Outdated
Comment thread tic-tac-toe-app/src/components/game/index.tsx Outdated
Comment thread tic-tac-toe-app/src/components/game/index.tsx Outdated
Comment thread tic-tac-toe-app/src/components/game/index.tsx Outdated
Comment thread tic-tac-toe-app/src/components/game/index.tsx Outdated
@ookura-mf

Copy link
Copy Markdown
Owner Author

とりあえずここまででcomponentに分けてfunctional componentに置き換えるところまでdone

Comment thread tic-tac-toe-app/src/components/game/GameHistoryButton.tsx
Comment thread tic-tac-toe-app/src/components/game/GameInfo.tsx
Comment thread tic-tac-toe-app/src/reducers/gameReducer.ts Outdated
@ookura-mf ookura-mf marked this pull request as ready for review December 6, 2021 13:25

return (
<div>
<div className="board-row">

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.

ここもcomponentにしてもよかったな・・・

Copy link
Copy Markdown

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.

ということでやってみました!9fd3816

@ookura-mf

Copy link
Copy Markdown
Owner Author

component一個ごとにディレクトリを分けてtestとかstorybookのファイル置けるようにすると見やすいかもしれないけど今回はやってない

@ookura-mf

Copy link
Copy Markdown
Owner Author

多分useCallback使えそうな箇所ありそう、と思いながら使い方わからなかったので使い所があれば教えてもらえると喜びます 🙏

};

export const Board = (props: BoardProps) => {
const renderSquare = (i: number) => {

Copy link
Copy Markdown

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.

多分これで直った?
9fd3816

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ですね
僕の問題意識とあってたかはわからないですが詳しくは
ydammatsu#2 (comment)

xIsNext: true,
};

export const Game = () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Type Inference良いですよね
僕は割と本気でこれくらいでいいと思っています


export const Game = () => {
const [state, dispatch] = useReducer(gameReducer, initialState);
const handleClick = (squareIndex: number) => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

useCallback使うとしたらこの辺りじゃないでしょうか

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.

useCallbackのモチベーションに関してはさっきのコメントが参考になるかもです
#2 (comment)

componentにしろfunctionにしろ(function componentとfunctionの違いはプログラム的な意味で言うとほとんどないんですが)function componentがupdateされる=functionが実行されるなのでその時にcomponent内で定義されてるfunctionが再定義されて問題ないかがポイントだと思います

問題になるケースの代表的なのがcomponent A内で定義しているfunction Aをどこかのcomponent Bに渡していてそのcomponent BがuseEffectでsetXXよんでそれが component Aをupdateするときに無限ループになるケース (case A)

空配列渡している場合はとりあえず無条件でmemoizeされることになる

useCallback(fn, deps) is equivalent to useMemo(() => fn, deps).

こうすることで case A みたいなことは防げるがそう言うモチベーションが無ければ基本的にfunctionって状態持っていないのでmemoizeするメリットは別にない(計算結果をmemoizeするのとは違う)

今の会計プラスの考え方だととりあえずcomponent内で定義したfunctionにはuseCallbackしておけばこういうリスク無くせるのでやっておこうという感じですね

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

基本的にfunctionって状態持っていないので

JSだとfunctionはObjectなので状態持たせられるけど

| { type: GameActionType.GAME_ACTION_ADD; squareIndex: number }
| { type: GameActionType.GAME_ACTION_JUMP; step: number };

export enum GameActionType {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こういう用途だとenumよりstringでunion type使う方が良いと思います
詳しくは
https://stackoverflow.com/questions/40275832/typescript-has-unions-so-are-enums-redundant

色々あるんですがunion typeの方が簡潔にかけて良いので

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

あとActionTypeをこうやってまとめるのは丁寧で良いと思いますがこんな書き方で十分事足ります

https://codesandbox.io/s/roulette-kob3l?file=/src/stateManagement/actions.ts

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.

修正してみましたー
879f136

@@ -0,0 +1,13 @@
export type SquareValue = "X" | "O" | null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的にこの型の名前微妙だと思うんですよね
変数名がSquareProps.valueに対して型がSquareValueで良いのかなと

あんまり細かいことは気にしないですがここは気になります
情報量ない命名になってる気がするのでコードの可読性が下がる気が

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.

これあんまりいい単語が思いつかなかったんでとりあえずtic tac toeのwikiからそれっぽい単語を引っ張ってきてはめてみました

Comment thread tic-tac-toe-app/src/reducers/gameReducer.ts
@@ -1,13 +1,13 @@
export type SquareValue = "X" | "O" | null;
export type Mark = "X" | "O" | null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

良いと思います 👍
僕もこうしました
後で若干nullを分けたくなるかもですがそんなに大した話ではないです

@Y4suyuki Y4suyuki left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

LGTMです 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants