-
-
Notifications
You must be signed in to change notification settings - Fork 397
refactor createUseStyles to work with react-hot-loader #1237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,13 +20,6 @@ const useEffectOrLayoutEffect = isInBrowser ? React.useLayoutEffect : React.useE | |
|
||
const noTheme = {} | ||
|
||
const reducer = (prevState, action) => { | ||
if (action.type === 'updateSheet') { | ||
return action.payload | ||
} | ||
return prevState | ||
} | ||
|
||
const createUseStyles = <Theme: {}>(styles: Styles<Theme>, options?: HookOptions<Theme> = {}) => { | ||
const {index = getSheetIndex(), theming, name, ...sheetOptions} = options | ||
const ThemeContext = (theming && theming.context) || DefaultThemeContext | ||
|
@@ -42,111 +35,75 @@ const createUseStyles = <Theme: {}>(styles: Styles<Theme>, options?: HookOptions | |
const context = React.useContext(JssContext) | ||
const theme = useTheme() | ||
|
||
const [state, dispatch] = React.useReducer(reducer, null, () => { | ||
const sheet = createStyleSheet({ | ||
context, | ||
styles, | ||
name, | ||
theme, | ||
index, | ||
sheetOptions | ||
}) | ||
|
||
let dynamicRules | ||
let classes | ||
if (sheet) { | ||
if (context.registry) { | ||
context.registry.add(sheet) | ||
} | ||
dynamicRules = addDynamicRules(sheet, data) | ||
classes = getSheetClasses(sheet, dynamicRules) | ||
} | ||
|
||
return { | ||
sheet, | ||
dynamicRules, | ||
classes: classes || {} | ||
} | ||
}) | ||
|
||
useEffectOrLayoutEffect( | ||
const [sheet, dynamicRules] = React.useMemo( | ||
() => { | ||
if (state.sheet) { | ||
const newSheet = createStyleSheet({ | ||
context, | ||
styles, | ||
name, | ||
theme, | ||
index, | ||
sheetOptions | ||
}) | ||
|
||
const newDynamicRules = newSheet ? addDynamicRules(newSheet, data) : null | ||
|
||
if (newSheet) { | ||
manageSheet({ | ||
index, | ||
context, | ||
sheet: state.sheet, | ||
sheet: newSheet, | ||
theme | ||
}) | ||
} | ||
|
||
return () => { | ||
const {sheet, dynamicRules} = state | ||
|
||
if (!sheet) return | ||
|
||
unmanageSheet({ | ||
index, | ||
context, | ||
sheet, | ||
theme | ||
}) | ||
|
||
if (dynamicRules) { | ||
removeDynamicRules(sheet, dynamicRules) | ||
} | ||
} | ||
return [newSheet, newDynamicRules] | ||
}, | ||
[state.sheet] | ||
[context, theme] | ||
) | ||
|
||
useEffectOrLayoutEffect( | ||
() => { | ||
// We only need to update the rules on a subsequent update and not in the first mount | ||
if (state.sheet && state.dynamicRules && !isFirstMount.current) { | ||
updateDynamicRules(data, state.sheet, state.dynamicRules) | ||
if (sheet && dynamicRules && !isFirstMount.current) { | ||
updateDynamicRules(data, sheet, dynamicRules) | ||
} | ||
}, | ||
[data] | ||
) | ||
|
||
useEffectOrLayoutEffect( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we just move this above the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That cleanup depends on the sheet argument, the one above on data. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems correct to me |
||
() => { | ||
if (!isFirstMount.current) { | ||
const newSheet = createStyleSheet({ | ||
context, | ||
styles, | ||
name, | ||
theme, | ||
index, | ||
sheetOptions | ||
}) | ||
const newDynamicRules = newSheet && addDynamicRules(newSheet, data) | ||
const newClasses = newSheet ? getSheetClasses(newSheet, newDynamicRules) : {} | ||
|
||
dispatch({ | ||
type: 'updateSheet', | ||
payload: { | ||
sheet: newSheet, | ||
dynamicRules: newDynamicRules, | ||
classes: newClasses | ||
} | ||
}) | ||
} | ||
}, | ||
[theme, context] | ||
() => | ||
// cleanup only | ||
() => { | ||
if (sheet) { | ||
unmanageSheet({ | ||
index, | ||
context, | ||
sheet, | ||
theme | ||
}) | ||
} | ||
|
||
if (sheet && dynamicRules) { | ||
removeDynamicRules(sheet, dynamicRules) | ||
} | ||
}, | ||
[sheet] | ||
) | ||
|
||
const classes = sheet && dynamicRules ? getSheetClasses(sheet, dynamicRules) : {} | ||
|
||
// $FlowFixMe | ||
React.useDebugValue(state.classes) | ||
React.useDebugValue(classes) | ||
// $FlowFixMe | ||
React.useDebugValue(theme === noTheme ? 'No theme' : theme) | ||
|
||
React.useEffect(() => { | ||
isFirstMount.current = false | ||
}) | ||
|
||
return state.classes | ||
return classes | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I remember correctly there was a problem with using
React.useMemo
as it can be rerun by React at a given point of time.I think @eps1lon knew more about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you are talking about the theoretical situation in async mode, not sure if it was made real.