Skip to content

Commit

Permalink
feat!: useId の独自実装を削除し、React 18 未満のサポートを終了する
Browse files Browse the repository at this point in the history
  • Loading branch information
s-sasaki-0529 committed Sep 13, 2024
1 parent a07d257 commit 7f96d3e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 36 deletions.
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",
"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()
}
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'

0 comments on commit 7f96d3e

Please sign in to comment.