Skip to content

Instantly share code, notes, and snippets.

@jxnblk
Created January 26, 2019 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jxnblk/b7d187d61d30583275ead37897d5130a to your computer and use it in GitHub Desktop.
Save jxnblk/b7d187d61d30583275ead37897d5130a to your computer and use it in GitHub Desktop.
import React from 'react'
export default class ServerStyled extends React.Component {
constructor (props) {
super(props)
const isServer = typeof document === 'undefined'
if (!isServer) return
const { ServerStyleSheet } = require('styled-components')
const { renderToString } = require('react-dom/server')
const sheet = new ServerStyleSheet()
renderToString(
sheet.collectStyles(props.children)
)
this.styles = sheet.getStyleElement()
}
render () {
if (!this.styles) return this.props.children
return (
<React.Fragment>
{this.styles}
{this.props.children}
</React.Fragment>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment