@@ -12,6 +12,7 @@ import {
12
12
import getSheetIndex from './utils/getSheetIndex'
13
13
import { manageSheet , unmanageSheet } from './utils/managers'
14
14
import getSheetClasses from './utils/getSheetClasses'
15
+ import memoize from './utils/memoizeOne'
15
16
16
17
const useEffectOrLayoutEffect = isInBrowser ? React . useLayoutEffect : React . useEffect
17
18
@@ -21,49 +22,57 @@ const createUseStyles = (styles, options = {}) => {
21
22
const { index = getSheetIndex ( ) , theming, name, ...sheetOptions } = options
22
23
const ThemeContext = ( theming && theming . context ) || DefaultThemeContext
23
24
24
- const useTheme = ( theme ) => {
25
+ const useTheme = theme => {
25
26
if ( typeof styles === 'function' ) {
26
27
return theme || React . useContext ( ThemeContext ) || noTheme
27
28
}
28
29
29
30
return noTheme
30
31
}
31
32
33
+ const memoizedGetSheetClasses = memoize ( getSheetClasses )
34
+
32
35
return function useStyles ( data ) {
33
36
const isFirstMount = React . useRef ( true )
34
37
const context = React . useContext ( JssContext )
35
38
const theme = useTheme ( data && data . theme )
36
39
37
- const [ sheet , dynamicRules ] = React . useMemo ( ( ) => {
38
- const newSheet = createStyleSheet ( {
39
- context,
40
- styles,
41
- name,
42
- theme,
43
- index,
44
- sheetOptions
45
- } )
46
-
47
- const newDynamicRules = newSheet ? addDynamicRules ( newSheet , data ) : null
48
-
49
- if ( newSheet ) {
50
- manageSheet ( {
51
- index,
40
+ const [ sheet , dynamicRules ] = React . useMemo (
41
+ ( ) => {
42
+ const newSheet = createStyleSheet ( {
52
43
context,
53
- sheet : newSheet ,
54
- theme
44
+ styles,
45
+ name,
46
+ theme,
47
+ index,
48
+ sheetOptions
55
49
} )
56
- }
57
50
58
- return [ newSheet , newDynamicRules ]
59
- } , [ context , theme ] )
51
+ const newDynamicRules = newSheet ? addDynamicRules ( newSheet , data ) : null
60
52
61
- useEffectOrLayoutEffect ( ( ) => {
62
- // We only need to update the rules on a subsequent update and not in the first mount
63
- if ( sheet && dynamicRules && ! isFirstMount . current ) {
64
- updateDynamicRules ( data , sheet , dynamicRules )
65
- }
66
- } , [ data ] )
53
+ if ( newSheet ) {
54
+ manageSheet ( {
55
+ index,
56
+ context,
57
+ sheet : newSheet ,
58
+ theme
59
+ } )
60
+ }
61
+
62
+ return [ newSheet , newDynamicRules ]
63
+ } ,
64
+ [ context , theme ]
65
+ )
66
+
67
+ useEffectOrLayoutEffect (
68
+ ( ) => {
69
+ // We only need to update the rules on a subsequent update and not in the first mount
70
+ if ( sheet && dynamicRules && ! isFirstMount . current ) {
71
+ updateDynamicRules ( data , sheet , dynamicRules )
72
+ }
73
+ } ,
74
+ [ data ]
75
+ )
67
76
68
77
useEffectOrLayoutEffect (
69
78
( ) =>
@@ -85,7 +94,7 @@ const createUseStyles = (styles, options = {}) => {
85
94
[ sheet ]
86
95
)
87
96
88
- const classes = sheet && dynamicRules ? getSheetClasses ( sheet , dynamicRules ) : { }
97
+ const classes = sheet && dynamicRules ? memoizedGetSheetClasses ( sheet , dynamicRules ) : { }
89
98
90
99
React . useDebugValue ( classes )
91
100
0 commit comments