-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use id fix and remove render hooks console error
- Loading branch information
1 parent
a3e242f
commit 339ddd7
Showing
5 changed files
with
70 additions
and
27 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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//Copied with minor modifications from https://github.com/reach/reach-ui/blob/dev/packages/auto-id/src/reach-auto-id.ts | ||
|
||
|
||
import * as React from "react"; | ||
import useIsomorphicLayoutEffect from 'use-isomorphic-layout-effect'; | ||
|
||
const useLayoutEffect = typeof useIsomorphicLayoutEffect === 'function' | ||
? useIsomorphicLayoutEffect | ||
: useIsomorphicLayoutEffect['default']; | ||
|
||
let serverHandoffComplete = false; | ||
let id = 0; | ||
function genId() { | ||
return ++id; | ||
} | ||
|
||
// Workaround for https://github.com/webpack/webpack/issues/14814 | ||
// https://github.com/eps1lon/material-ui/blob/8d5f135b4d7a58253a99ab56dce4ac8de61f5dc1/packages/mui-utils/src/useId.ts#L21 | ||
const maybeReactUseId: undefined | (() => string) = (React as any)[ | ||
"useId".toString() | ||
]; | ||
|
||
/** | ||
* useId | ||
* | ||
* Autogenerate IDs to facilitate WAI-ARIA and server rendering. | ||
* | ||
* Note: The returned ID will initially be empty string and will update after a | ||
* component mounts. | ||
* | ||
* @see Docs https://reach.tech/auto-id | ||
*/ | ||
|
||
export function useId():string { | ||
if (maybeReactUseId !== undefined) { | ||
return maybeReactUseId(); | ||
} | ||
|
||
// If this instance isn't part of the initial render, we don't have to do the | ||
// double render/patch-up dance. We can just generate the ID and return it. | ||
const initialId = (serverHandoffComplete ? genId() : ''); | ||
const [id, setId] = React.useState(initialId); | ||
|
||
useLayoutEffect(() => { | ||
if (id === '') { | ||
// Patch the ID after render. We do this in `useLayoutEffect` to avoid any | ||
// rendering flicker, though it'll make the first render slower (unlikely | ||
// to matter, but you're welcome to measure your app and let us know if | ||
// it's a problem). | ||
setId(genId()); | ||
} | ||
}, [id]); | ||
|
||
React.useEffect(() => { | ||
if (serverHandoffComplete === false) { | ||
// Flag all future uses of `useId` to skip the update dance. This is in | ||
// `useEffect` because it goes after `useLayoutEffect`, ensuring we don't | ||
// accidentally bail out of the patch-up dance prematurely. | ||
serverHandoffComplete = true; | ||
} | ||
}, []); | ||
|
||
return id.toString(); | ||
} |
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