-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate unique ids within each React island (#6976)
- Loading branch information
Showing
7 changed files
with
71 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/react': patch | ||
--- | ||
|
||
Prevent ID collisions in React.useId |
6 changes: 6 additions & 0 deletions
6
packages/astro/test/fixtures/react-component/src/components/WithId.jsx
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,6 @@ | ||
import React from 'react'; | ||
|
||
export default function () { | ||
const id = React.useId(); | ||
return <p className='react-use-id' id={id}>{id}</p>; | ||
} |
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,24 @@ | ||
const contexts = new WeakMap(); | ||
|
||
const ID_PREFIX = 'r'; | ||
|
||
function getContext(rendererContextResult) { | ||
if (contexts.has(rendererContextResult)) { | ||
return contexts.get(rendererContextResult); | ||
} | ||
const ctx = { | ||
currentIndex: 0, | ||
get id() { | ||
return ID_PREFIX + this.currentIndex.toString(); | ||
}, | ||
}; | ||
contexts.set(rendererContextResult, ctx); | ||
return ctx; | ||
} | ||
|
||
export function incrementId(rendererContextResult) { | ||
const ctx = getContext(rendererContextResult) | ||
const id = ctx.id; | ||
ctx.currentIndex++; | ||
return id; | ||
} |
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