Created
January 26, 2019 20:58
-
-
Save jxnblk/b7d187d61d30583275ead37897d5130a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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