-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: useId の独自実装を削除し、React 18 未満のサポートを終了する
- Loading branch information
1 parent
a07d257
commit 7f96d3e
Showing
3 changed files
with
5 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters