Skip to content

[popups] useTriggerRegistration throws error when react version < 18 #3300

@mkhu-scc

Description

@mkhu-scc

Bug report

Current behavior

Base UI: Trigger must have an id prop specified.

Image

Expected behavior

support react 17

Reproducible example

StackBlitz

Base UI version

1.0.0-beta.6

Additional context

export function useTriggerRegistration(id: string | undefined) {
  const [element, setElement] = React.useState<HTMLElement | null>(null);

  useIsoLayoutEffect(() => {
    if (id == null) {
      if (element != null) {
        throw new Error('Base UI: Trigger must have an `id` prop specified.');
      }
      return;
    }
  }, [element, id, store]);
}
function useGlobalId(idOverride?: string, prefix: string = 'mui'): string | undefined {
  const [defaultId, setDefaultId] = React.useState(idOverride);
  const id = idOverride || defaultId;
  React.useEffect(() => {
    if (defaultId == null) {
      // Fallback to this default id when possible.
      // Use the incrementing value for client-side rendering only.
      // We can't use it server-side.
      // If you want to use random values please consider the Birthday Problem: https://en.wikipedia.org/wiki/Birthday_problem
      globalId += 1;
      setDefaultId(`${prefix}-${globalId}`);
    }
  }, [defaultId, prefix]);
  return id;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    component: menuChanges related to the menu component.component: popoverChanges related to the popover component.component: tooltipChanges related to the tooltip component.type: regressionA bug, but worse, it used to behave as expected.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions