-
Notifications
You must be signed in to change notification settings - Fork 141
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここ、 修正前 ( コンポーネント内で There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @s-sasaki-0529 上記でいただいているコードのほうが良いと思いました! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 厳密には挙動が変わることになるからためらってたんですが、少なくとも悪化することは無さそうなのでそうしちゃいます! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらもリポジトリ内及び社内では一切使用されていなかったのであわせて削除してます。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
事実上、React 17 以下で使えないコンポーネントが生まれることになるのでここも変えちゃってよいですよね。
のでこっちのほうがインパクト強いという意味で、PRタイトルに含めてます。