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

feat!: useId の独自実装を削除し、React 18 未満のサポートを終了する #4920

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions packages/smarthr-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@
"webpack": "^5.94.0"
},
"peerDependencies": {
"react": "16.13.0 || ^17.0.1 || ^18.0.0",
"react-dom": "16.13.0 || ^17.0.1 || ^18.0.0",
"react": "^18.0.0",
"react-dom": "^18.0.0",
Copy link
Contributor Author

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

Choose a reason for hiding this comment

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

事実上、React 17 以下で使えないコンポーネントが生まれることになるのでここも変えちゃってよいですよね。
のでこっちのほうがインパクト強いという意味で、PRタイトルに含めてます。

"styled-components": "^5.0.1"
},
"bugs": {
Expand Down
34 changes: 3 additions & 31 deletions packages/smarthr-ui/src/hooks/useId.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
import React, { ReactNode, VFC, createContext, useContext, useMemo } from 'react'

type IdContextValue = {
prefix: number
current: number
}

const defaultContext: IdContextValue = {
prefix: 0,
current: 0,
}

const IdContext = createContext<IdContextValue>(defaultContext)

function useId_OLD() {
const context = useContext(IdContext)
return useMemo(() => `id-${context.prefix}-${++context.current}`, [context])
}
import React from 'react'

export const useId = (defaultId?: string): string => {
if (defaultId) return defaultId

// React v18 以降は React.useId を使う
return ('useId' in React ? React.useId : useId_OLD)()
}

export const SequencePrefixIdProvider: VFC<{ children: ReactNode }> = ({ children }) => {
const context = useContext(IdContext)
// increment `prefix` and reset `current` to 0 on every Provider
const value: IdContextValue = {
prefix: context.prefix + 1,
current: 0,
}
return <IdContext.Provider value={value}>{children}</IdContext.Provider>
// eslint-disable-next-line react-hooks/rules-of-hooks
return React.useId()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ここ、 if (defaultId) 分岐の下に Hooks が登場するので、Hooks の制約違反で警告が出てしまうので無視してます。

修正前 (return ('useId' in React ? React.useId : useId_OLD)()) は動的にコードを組み立ててた都合、さすがの ESLint も検知できなかったようです。

コンポーネント内で defaultId を渡すか渡さないかが描画ごとに異なる場合におかしな挙動になる可能性があるなぁと感じますが、これまで異常がなかったので大丈夫かなと思ってます。

Copy link
Contributor Author

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

Choose a reason for hiding this comment

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

あるいはこうすることで回避するほうが安全かも…?

export const useId = (defaultId?: string): string => {
  const id = React.useId()
  return defaultId || id
}

Copy link
Contributor

@atzzCokeK atzzCokeK Sep 17, 2024

Choose a reason for hiding this comment

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

@s-sasaki-0529 上記でいただいているコードのほうが良いと思いました!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

厳密には挙動が変わることになるからためらってたんですが、少なくとも悪化することは無さそうなのでそうしちゃいます!

}
3 changes: 0 additions & 3 deletions packages/smarthr-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,3 @@ export { defaultBreakpoint } from './themes/createBreakpoint'

// constants
export { FONT_FAMILY, CHART_COLORS, OTHER_CHART_COLOR } from './constants'

// utils
export { SequencePrefixIdProvider } from './hooks/useId'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

こちらもリポジトリ内及び社内では一切使用されていなかったのであわせて削除してます。