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

有考虑增加在server端支持styled-component的同构 #8

Open
ghost opened this issue Aug 5, 2020 · 0 comments
Open

有考虑增加在server端支持styled-component的同构 #8

ghost opened this issue Aug 5, 2020 · 0 comments

Comments

@ghost
Copy link

ghost commented Aug 5, 2020

一直在使用easywebpack,项目有场景在运行时对样式的操作/变量的传值,需要使用styled-component,但在server端进行renderToString收集好component的样式表,append进

import { renderToString } from 'react-dom/server'
import { ServerStyleSheet } from 'styled-components'

const sheet = new ServerStyleSheet()
try {
  const html = renderToString(sheet.collectStyles(<YourApp />))
  const styleTags = sheet.getStyleTags() // or sheet.getStyleElement();
} catch (error) {
  // handle error
  console.error(error)
} finally {
  sheet.seal()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants