Skip to content

Commit

Permalink
chore: Hooks のベストプラクティスに沿って修正
Browse files Browse the repository at this point in the history
  • Loading branch information
s-sasaki-0529 committed Sep 17, 2024
1 parent 7f96d3e commit 38bd8ee
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
25 changes: 25 additions & 0 deletions packages/smarthr-ui/src/hooks/useId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { renderHook } from '@testing-library/react'

import { useId } from './useId'

describe('useId', () => {
it('defaultId を指定した場合、毎回同じ値を返す', () => {
const { result: id1 } = renderHook(() => useId('test'))
const { result: id2 } = renderHook(() => useId('test'))
const { result: id3 } = renderHook(() => useId('test'))

expect(id1.current).toEqual('test')
expect(id2.current).toEqual('test')
expect(id3.current).toEqual('test')
})

it('defaultId を指定しない場合、異なる値を返す', () => {
const { result: id1 } = renderHook(() => useId())
const { result: id2 } = renderHook(() => useId())
const { result: id3 } = renderHook(() => useId())

expect(id1.current).not.toEqual(id2.current)
expect(id2.current).not.toEqual(id3.current)
expect(id3.current).not.toEqual(id1.current)
})
})
5 changes: 2 additions & 3 deletions packages/smarthr-ui/src/hooks/useId.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react'

export const useId = (defaultId?: string): string => {
if (defaultId) return defaultId
// eslint-disable-next-line react-hooks/rules-of-hooks
return React.useId()
const id = React.useId()
return defaultId || id
}

0 comments on commit 38bd8ee

Please sign in to comment.