Skip to content

Commit 5ddef29

Browse files
giuseppegtimneutkens
authored andcommitted
Fix JSXStyle renders styles too late (#484)
We tried master on zeit.co and it seems that when we have the side effects in `componentDidMount` styles are rendered after the content causing transitions to apply to properties that wouldn't otherwise be animated e.g. `padding` similarly to this https://jsfiddle.net/uwbm165z/ (unfortunately I wasn't able to reproduce with React yet but here is my attempt https://codesandbox.io/s/p7q6n6r35m). In this patch we move the side effect `styleSheetRegistry.add` to `render` making sure that `shouldComponentUpdate` doesn't trigger unnecessary re-renderings. On update we then remove the old styles early enough aka on `getSnapshotBeforeUpdate`. @bvaughn this is a "followup" of the conversation we had in #457 (comment) Does it make sense?
1 parent 9f53a74 commit 5ddef29

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/style.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ import StyleSheetRegistry from './stylesheet-registry'
44
const styleSheetRegistry = new StyleSheetRegistry()
55

66
export default class JSXStyle extends Component {
7-
constructor(props) {
8-
super(props)
9-
10-
// SeverSideRendering only
11-
if (typeof window === 'undefined') {
12-
styleSheetRegistry.add(this.props)
13-
}
14-
}
15-
167
static dynamic(info) {
178
return info
189
.map(tagInfo => {
@@ -23,23 +14,32 @@ export default class JSXStyle extends Component {
2314
.join(' ')
2415
}
2516

26-
componentDidMount() {
27-
styleSheetRegistry.add(this.props)
28-
}
29-
17+
// probably faster than PureComponent (shallowEqual)
3018
shouldComponentUpdate(nextProps) {
31-
return this.props.css !== nextProps.css
19+
return (
20+
this.props.styleId !== nextProps.styleId ||
21+
// We do this check because `dynamic` is an array of strings or undefined.
22+
// These are the computed values for dynamic styles.
23+
String(this.props.dynamic) !== String(nextProps.dynamic)
24+
)
3225
}
3326

34-
componentDidUpdate(prevProps) {
35-
styleSheetRegistry.update(prevProps, this.props)
27+
// Remove styles in advance.
28+
getSnapshotBeforeUpdate(prevProps) {
29+
styleSheetRegistry.remove(prevProps)
30+
return null
3631
}
3732

33+
// Including this otherwise React complains that getSnapshotBeforeUpdate
34+
// is used without componentDidMount.
35+
componentDidUpdate() {}
36+
3837
componentWillUnmount() {
3938
styleSheetRegistry.remove(this.props)
4039
}
4140

4241
render() {
42+
styleSheetRegistry.add(this.props)
4343
return null
4444
}
4545
}

0 commit comments

Comments
 (0)