Skip to content

Commit 8053466

Browse files
authored
fix(useStyles): manageSheet must be in the same effect as unmanageSheet (#1609)
* managesheet must be in seperate effect * manage immediately during SSRs * size snapshot * actually use context.isSSR
1 parent c40e67c commit 8053466

2 files changed

Lines changed: 27 additions & 18 deletions

File tree

packages/react-jss/.size-snapshot.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
{
22
"react-jss.js": {
3-
"bundled": 136245,
4-
"minified": 50224,
5-
"gzipped": 16773
3+
"bundled": 136547,
4+
"minified": 50287,
5+
"gzipped": 16786
66
},
77
"react-jss.min.js": {
8-
"bundled": 103195,
9-
"minified": 40083,
10-
"gzipped": 13886
8+
"bundled": 103497,
9+
"minified": 40146,
10+
"gzipped": 13904
1111
},
1212
"react-jss.cjs.js": {
13-
"bundled": 21759,
14-
"minified": 9601,
15-
"gzipped": 3220
13+
"bundled": 22047,
14+
"minified": 9671,
15+
"gzipped": 3230
1616
},
1717
"react-jss.esm.js": {
18-
"bundled": 19855,
19-
"minified": 8114,
20-
"gzipped": 3000,
18+
"bundled": 20143,
19+
"minified": 8184,
20+
"gzipped": 3010,
2121
"treeshaked": {
2222
"rollup": {
2323
"code": 442,

packages/react-jss/src/createUseStyles.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ const createUseStyles = (styles, options = {}) => {
5050
sheetOptions
5151
})
5252

53-
if (newSheet) {
53+
if (newSheet && context.isSSR) {
54+
// manage immediately during SSRs. browsers will manage the sheet through useInsertionEffect below
5455
manageSheet({
5556
index,
5657
context,
@@ -69,8 +70,17 @@ const createUseStyles = (styles, options = {}) => {
6970
}
7071
}, [data])
7172

72-
getUseInsertionEffect(context.isSSR)(
73-
() => () => {
73+
getUseInsertionEffect(context.isSSR)(() => {
74+
if (sheet) {
75+
manageSheet({
76+
index,
77+
context,
78+
sheet,
79+
theme
80+
})
81+
}
82+
83+
return () => {
7484
if (sheet) {
7585
unmanageSheet({
7686
index,
@@ -84,9 +94,8 @@ const createUseStyles = (styles, options = {}) => {
8494
removeDynamicRules(sheet, dynamicRules)
8595
}
8696
}
87-
},
88-
[sheet]
89-
)
97+
}
98+
}, [sheet])
9099

91100
const classes = React.useMemo(
92101
() => (sheet && dynamicRules ? getSheetClasses(sheet, dynamicRules) : emptyObject),

0 commit comments

Comments
 (0)