fix: remove react identifierPrefix from client:only (#8075)

This was causing React components rendered with client:only
to be prefixed with null. While not technically causing any
issues, it is unintended and could be considered a bug.

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
This commit is contained in:
Robin Neal 2023-08-14 17:26:03 +01:00 committed by GitHub
parent 7d7920ea91
commit da517d4055
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/react': patch
---
fix a bug where react identifierPrefix was set to null for client:only components causing React.useId to generate ids prefixed with null

View file

@ -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>;
}

View file

@ -2,6 +2,7 @@
import Counter from '../components/Counter.jsx'; import Counter from '../components/Counter.jsx';
import ReactComponent from '../components/JSXComponent.jsx'; import ReactComponent from '../components/JSXComponent.jsx';
import Suffix from '../components/Suffix.react'; import Suffix from '../components/Suffix.react';
import WithId from '../components/WithId.jsx';
const someProps = { const someProps = {
count: 0, count: 0,
@ -36,5 +37,11 @@ const someProps = {
<ReactComponent id="client-only" client:only="react" /> <ReactComponent id="client-only" client:only="react" />
<Suffix client:load /> <Suffix client:load />
<WithId />
<WithId client:load />
<WithId client:load />
<WithId client:only="react" />
<WithId client:only="react" />
</body> </body>
</html> </html>

View file

@ -34,3 +34,22 @@ test.describe('dev', () => {
expect(await suffix.textContent()).toBe('suffix toggle true'); expect(await suffix.textContent()).toBe('suffix toggle true');
}); });
}); });
test.describe('React client id generation', () => {
test('react components generate unique ids', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/'));
const components = page.locator('.react-use-id');
await expect(components).toHaveCount(5);
const staticId = await components.nth(0).getAttribute('id');
const hydratedId0 = await components.nth(1).getAttribute('id');
const hydratedId1 = await components.nth(2).getAttribute('id');
const clientOnlyId0 = await components.nth(3).getAttribute('id');
const clientOnlyId1 = await components.nth(4).getAttribute('id');
console.log("ho ho", staticId, hydratedId0, hydratedId1, clientOnlyId0, clientOnlyId1)
expect(staticId).not.toEqual(hydratedId0)
expect(hydratedId0).not.toEqual(hydratedId1)
expect(hydratedId1).not.toEqual(clientOnlyId0)
expect(clientOnlyId0).not.toEqual(clientOnlyId1)
});
})

View file

@ -31,7 +31,7 @@ export default (element) =>
} }
if (client === 'only') { if (client === 'only') {
return startTransition(() => { return startTransition(() => {
createRoot(element, renderOptions).render(componentEl); createRoot(element).render(componentEl);
}); });
} }
return startTransition(() => { return startTransition(() => {