Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: [RadioButton]コンポーネントのpropsやstateに依存関係のないものは関数外で定義した #4936

Closed
wants to merge 1 commit into from

Conversation

hiroki0525
Copy link
Contributor

@hiroki0525 hiroki0525 commented Sep 19, 2024

関連URL

概要

タイトルの通りで、特にコンポーネント内部で定義する必要がないものについてはコンポーネント外で定義しました。
再レンダリング時に再評価されるのを防いだり、直接的にコンポーネントのpropsやstateの依存関係のない静的なものはコンポーネント外で出すことでコードを見やすくするため。

変更内容

マージ後なにも影響はない想定です。

確認方法

@hiroki0525 hiroki0525 requested a review from a team as a code owner September 19, 2024 02:14
@hiroki0525 hiroki0525 requested review from misako0927 and uknmr and removed request for a team September 19, 2024 02:14
Copy link

pkg-pr-new bot commented Sep 19, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/kufu/smarthr-ui@4936

commit: b67a4b6

@@ -52,18 +52,20 @@ const radioButton = tv({
},
})

const { wrapper, innerWrapper, box, input, label } = radioButton()
const innerWrapperStyle = innerWrapper()
const inputStyle = input()
Copy link
Contributor

@s-sasaki-0529 s-sasaki-0529 Sep 19, 2024

Choose a reason for hiding this comment

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

[imo]

良いような気もするんですけど、variant 使うかどうかで外と中どっちで定義するか変わってくるのはちょっとややこしいような気もしました。

再レンダリングコストについても、元々 useMemo 内にあるので基本的には再評価されませんしまぁ…。
ただ tailwind-variants 的なお作法の視点での良し悪しはわからない…。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

variant 使うかどうかで外と中どっちで定義するか変わってくるのはちょっとややこしいような気もしました。
tailwind-variants 的なお作法の視点での良し悪しはわからない…。

この辺は確かにあるかもです...うーん

再レンダリングコストについても、元々 useMemo 内にあるので基本的には再評価されませんしまぁ…。

一応厳密にいうと、下記のように再評価のプロセスが変わる認識ですね

before:

  1. useMemoで定義している依存配列に変更
  2. radioButton が評価
  3. innerWrapperinputStyle が評価

after:

  1. useMemoで定義している依存配列に変更
  2. radioButtonは評価されない。innerWrapper inputStyle も評価されない

ただ、まぁ細かいチューニングの類になると思うので、冒頭の可読性との兼ね合いかなぁ...と思います。
今回の変更にめちゃ強い気持ちもないので、他のレビュアーの方の意見も伺いたいですね

Copy link
Contributor

Choose a reason for hiding this comment

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

めちゃむずい・・・となってます🤔
今後全部この方針でいくんだ!というのであれば直してもいいと思いますが、今ほぼ全てのコンポーネントでuseMemo内にあったりするので、可読性を考えるとここだけ直すモチベは薄いかなぁと思いましたがどうでしょうか🧐

Copy link
Contributor Author

Choose a reason for hiding this comment

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

一応処理速度上がったが計測したのですが、予想に反して遅くなりました😇
今回対応した関数外に出したパターンが3ms、もともとあった useMemo 内で定義するパターンが一回の useMemo による再評価あたり 0.1 ms前後でした
だいぶモチベも薄くなってきたのでクローズしようと思います!

@hiroki0525 hiroki0525 closed this Sep 20, 2024
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.

3 participants