Last active
August 29, 2015 14:18
-
-
Save asbjornenge/39cd1bbb8f6a20e03591 to your computer and use it in GitHub Desktop.
React ScopeStyle
This file contains 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' | |
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> | |
) | |
} | |
} |
This file contains 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' | |
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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...