diff options
author | 2023-05-04 15:23:00 +0100 | |
---|---|---|
committer | 2023-05-04 15:23:00 +0100 | |
commit | ca329bbcae7a6075af4f428f6f64466e9d152c8f (patch) | |
tree | 0139a99c0cc60b22758e75ebb519893a5447ebb2 /packages/integrations/react/context.js | |
parent | dfb9e4270a7c05c292a549777408278fcbe162ab (diff) | |
download | astro-ca329bbcae7a6075af4f428f6f64466e9d152c8f.tar.gz astro-ca329bbcae7a6075af4f428f6f64466e9d152c8f.tar.zst astro-ca329bbcae7a6075af4f428f6f64466e9d152c8f.zip |
Generate unique ids within each React island (#6976)
Diffstat (limited to 'packages/integrations/react/context.js')
-rw-r--r-- | packages/integrations/react/context.js | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/packages/integrations/react/context.js b/packages/integrations/react/context.js new file mode 100644 index 000000000..5d9b1d7b1 --- /dev/null +++ b/packages/integrations/react/context.js @@ -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; +} |