Description
The problem
Every time a styled
component is rendered, it needs to serialize the styles even if they are the same from the previous render. That can be a hit in the performance when there dozens of instances of the same component and the styles need to do heavy transformations on the prop values, e.g. manipulate colors.
Proposed solution
Cache the serialized styles and return the same values if the cache key provided by the user didn't change. The cache key could be the "dependencies" of the styles.
Alternative solutions
Calling css
inside React.useMemo
works but in my context I can't call css
directly.
Additional context
We're building a data grid where every cell is a styled component. Virtualization is already being used to recycle DOM elements. Although, on every scroll React has to render all nodes to compute the diff. The styles provided to each cell call some color manipulation functions which cost a bit of processing. React.memo
can't be used because there're props on each cell that changes during scroll, but are not used in the styles.