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.
Problem
useThemememoizes its resolvedtokensmap, but creates a newtokenfunction and return object on every render:This defeats memoization in consumers. For example,
useChartColorsmemoizes its palette fromtoken, 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
tokenfrom the resolvedtokensmapUseThemeReturnobject from its exposed membersRegression coverage
A test rerenders
useThemewith 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.