Skip to content
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

Fix JSXStyle renders styles too late #484

Merged
merged 1 commit into from
Aug 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ import StyleSheetRegistry from './stylesheet-registry'
const styleSheetRegistry = new StyleSheetRegistry()

export default class JSXStyle extends Component {
constructor(props) {
super(props)

// SeverSideRendering only
if (typeof window === 'undefined') {
styleSheetRegistry.add(this.props)
}
}

static dynamic(info) {
return info
.map(tagInfo => {
Expand All @@ -23,23 +14,32 @@ export default class JSXStyle extends Component {
.join(' ')
}

componentDidMount() {
styleSheetRegistry.add(this.props)
}

// probably faster than PureComponent (shallowEqual)
shouldComponentUpdate(nextProps) {
return this.props.css !== nextProps.css
return (
this.props.styleId !== nextProps.styleId ||
// We do this check because `dynamic` is an array of strings or undefined.
// These are the computed values for dynamic styles.
String(this.props.dynamic) !== String(nextProps.dynamic)
)
}

componentDidUpdate(prevProps) {
styleSheetRegistry.update(prevProps, this.props)
// Remove styles in advance.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what this means or what it's doing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On update we then remove the old styles early enough aka on getSnapshotBeforeUpdate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the case when you have dynamic styles i.e. interpolations (tmpl literals expressions) that are based on props

getSnapshotBeforeUpdate(prevProps) {
styleSheetRegistry.remove(prevProps)
return null
}

// Including this otherwise React complains that getSnapshotBeforeUpdate
// is used without componentDidMount.
componentDidUpdate() {}

componentWillUnmount() {
styleSheetRegistry.remove(this.props)
}

render() {
styleSheetRegistry.add(this.props)
return null
}
}
Expand Down