Skip to content

[Bug] useTheme returns unstable token resolver and API object #5273

Description

@nynexman4464

Problem

useTheme memoizes its resolved tokens map, but creates a new token function and return object on every render:

const token = (name: string) => tokens[name] ?? '';
return {name, mode, token, tokens};

This defeats memoization in consumers. For example, useChartColors memoizes its palette from token, but the changing resolver causes it to rebuild the full color API after every unrelated rerender. An effect depending on that API reruns even though the theme and mode did not change.

Proposed fix

  • memoize token from the resolved tokens map
  • memoize the returned UseThemeReturn object from its exposed members
  • preserve identity changes when the theme or effective mode actually changes

Regression coverage

A test rerenders useTheme with no theme change and verifies both the token resolver and return object remain stable. The test fails on current main and passes with the patch.

No public types or resolved token values change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions