cssinjs / cssinjs/jss

Stylesheet.classes is never reused, even for static styles

Open
#1,347 4 comments 0 reactions 0 assignees View on GitHub
bug enhancement important perf
Dominant language
JavaScript
Stars
7.1k
Forks
386
PR merge metrics
No merged PRs in 30d

Description

__Expected behavior:__
I am using react-jss.
I use a totally static style declaration object, passed once to createUseStyles, but consumed by many components (using useStyles). I expect all components consuming the generated styles to receive the same classes object instance. In fact a new instance of the classes object is generated for every consuming component (all with identical properties).

__Describe the bug:__
Calls to addDynamicRules always return a truthy value (an empty object literal) even when there are no dynamic rules. This is subsequently tested and, because truthy, new classes object is always created.

call to addDynamicRules always returns object for dynamicRules even when there are no dynamic
rules, hence dynamicRules is truthy from now on

from `utils/sheets.js`
````javascript
export const addDynamicRules = (sheet: StyleSheet, data: any): ?DynamicRules => {
const meta = getMeta(sheet)
// Note meta looks like this {styles: object, dynamicStyles: null }
if (!meta) {
// I believe this should also return here if dynamicStyles is null ?
return undefined
}
... continue and return empty object as dynamic rules
}
````
In the following snippet, dynamicRules is required to be truthy before getSheetClasses is called, but getSheetClasses will only return existing classes without recalculation if dynamivStyles is falsey !

from `createUseStyles.js`
````javascript
const classes = sheet && dynamicRules ? getSheetClasses(sheet, dynamicRules) : {}
````

__Codesandbox link:__
https://codesandbox.io/s/recursing-jepsen-pol0s

__Versions (please complete the following information):__
- jss: react-jss: 10.1.1
- Browser: chrome
- OS: macOS
Feel free to add any additional versions which you may think are relevant to the bug.

Happy to submit a PR if confirmed that this is. a bug and not intended behaviour.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.