mirror of https://github.com/grafana/grafana
Chore: Move useUniqueId to a general place (#69542)
* Move useUniqueId to a general place * Use new built-in useId hookpull/69558/head
parent
8914b8b96f
commit
97221595c1
@ -1,24 +0,0 @@ |
||||
import { renderHook } from '@testing-library/react'; |
||||
|
||||
import { useUniqueId } from './useUniqueId'; |
||||
|
||||
describe('useUniqueId', () => { |
||||
it('should work correctly', () => { |
||||
const { result: resultA, rerender: rerenderA } = renderHook(() => useUniqueId()); |
||||
const { result: resultB, rerender: rerenderB } = renderHook(() => useUniqueId()); |
||||
|
||||
// the values of the separate hooks should be different
|
||||
expect(resultA.current).not.toBe(resultB.current); |
||||
|
||||
// we copy the current values after the first render
|
||||
const firstValueA = resultA.current; |
||||
const firstValueB = resultB.current; |
||||
|
||||
rerenderA(); |
||||
rerenderB(); |
||||
|
||||
// we check that the value did not change
|
||||
expect(resultA.current).toBe(firstValueA); |
||||
expect(resultB.current).toBe(firstValueB); |
||||
}); |
||||
}); |
@ -1,17 +0,0 @@ |
||||
import { uniqueId } from 'lodash'; |
||||
import { useRef } from 'react'; |
||||
|
||||
export function useUniqueId(): string { |
||||
// we need to lazy-init this ref.
|
||||
// otherwise we would call `uniqueId`
|
||||
// on every render. unfortunately
|
||||
// useRef does not have lazy-init builtin,
|
||||
// like useState does. we do it manually.
|
||||
const idRefLazy = useRef<string | null>(null); |
||||
|
||||
if (idRefLazy.current == null) { |
||||
idRefLazy.current = uniqueId(); |
||||
} |
||||
|
||||
return idRefLazy.current; |
||||
} |
Loading…
Reference in new issue