Skip to content

Nested SSRProviders don't guarantee unique IDs  #2277

@jfuchs

Description

@jfuchs

🐛 Bug Report

Given this tree of SSR providers (cribbing from SSRProvider.test.js):

<SSRProvider>
  <SSRProvider>
    <Test />
    <SSRProvider>
      <Test />
    </SSRProvider>
  </SSRProvider>
  <SSRProvider>
    <Test />
  </SSRProvider>
</SSRProvider>

Each <Test>, when calling useId, should get a unique ID, but two of those tests do not.

🤔 Expected Behavior

I don't have a preference for what the IDs are so long as they are unique and namespaced and deterministic.

😯 Current Behavior

Here's a snapshot test showing the current behavior:

it("it should generate consistent unique ids with nested SSR providers", function () {
  let tree = render(
    <SSRProvider>
      <SSRProvider>
        <Test />
        <SSRProvider>
          <Test />
        </SSRProvider>
      </SSRProvider>
      <SSRProvider>
        <Test />
      </SSRProvider>
    </SSRProvider>
  );

  let divs = tree.getAllByTestId("test");
  expect(divs).toMatchInlineSnapshot(`
    Array [
      <div
        data-testid="test"
        id="react-aria-1-1"
      />,
      <div
        data-testid="test"
        id="react-aria-2-1"
      />,
      <div
        data-testid="test"
        id="react-aria-2-1"
      />,
    ]
  `);
});

💁 Possible Solution

I believe a possible fix would be making each ID essentially a path through the tree of SSRProviders, so that for the above example, the IDs would be:

react-aria-1-1
react-aria-1-1-1
react-aria-1-2

🔦 Context

I stumbled into this looking at different implementations for SSR-stable unique ID generation in react.

💻 Code Sample

See the current behavior above

🌍 Your Environment

I was running tests on main as of 2021 Aug 12 (v3.0.3 of @react-aria/ssr)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions