Skip to content

Commit 15ce6c4

Browse files
committed
perf(Text): prevent unnecessary re-renders by memoizing style transform
Use useCallback to cache the transform function in Text.tsx, preventing unnecessary re-renders and diffing operations in the underlying reconciler when style props remain unchanged.
1 parent 014ff4a commit 15ce6c4

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

src/components/Text.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useContext, type ReactNode } from "react";
1+
import { useContext, useCallback, type ReactNode } from "react";
22
import {
33
applyTextStyles,
44
type TextStyles,
@@ -90,18 +90,30 @@ export function Text({
9090
return null;
9191
}
9292

93-
const transform = (children: string): string => {
94-
return applyTextStyles(children, {
93+
const transform = useCallback(
94+
(children: string): string => {
95+
return applyTextStyles(children, {
96+
color,
97+
backgroundColor: effectiveBackgroundColor,
98+
dimColor,
99+
bold,
100+
italic,
101+
underline,
102+
strikethrough,
103+
inverse,
104+
});
105+
},
106+
[
95107
color,
96-
backgroundColor: effectiveBackgroundColor,
108+
effectiveBackgroundColor,
97109
dimColor,
98110
bold,
99111
italic,
100112
underline,
101113
strikethrough,
102114
inverse,
103-
});
104-
};
115+
],
116+
);
105117

106118
if (isScreenReaderEnabled && ariaHidden) {
107119
return null;

0 commit comments

Comments
 (0)