Skip to content

Instantly share code, notes, and snippets.

@asbjornenge
Last active August 29, 2015 14:18
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 asbjornenge/39cd1bbb8f6a20e03591 to your computer and use it in GitHub Desktop.
Save asbjornenge/39cd1bbb8f6a20e03591 to your computer and use it in GitHub Desktop.
React ScopeStyle
import React from 'react'
import ScopeStyle from 'react-scopestyle'
let css = "div { color:pink; }"
export default class MyScopeStyleComponent extends React.Component {
render() {
return (
<ScopeStyle style={css}>
<div>yolo</div>
</ScopeStyle>
)
}
}
import React from 'react'
import uid from 'uid'
export default class ScopeStyle extends React.Component {
constructor(props) {
super(props)
this.state = { uid : uid() }
}
render() {
return (
<span className={this.state.uid}>
<style dangerouslySetInnerHTML={{__html: this.scopeStyle()}} />
{this.props.children}
</span>
)
}
scopeStyle() {
// magically scope under .uid
return this.props.style
}
}
@asbjornenge
Copy link
Author

Will scope style for component under .uid so div styling only applies to the yolo div. However, constantly adding and removing style tags has some performance issues. Not too bad, but still...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment