Last active
September 12, 2019 05:16
-
-
Save mikkel/8b79a713ff06bbec379d to your computer and use it in GitHub Desktop.
Import an SVG file into your DOM with React and ES6. Useful for manipulating internal path elements. Only use trusted sources.
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
var React = require('react'); | |
class SvgImage extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = {}; | |
} | |
componentDidMount() { | |
$.get(this.props.url, (svg) => { | |
this.setState({icon: svg.documentElement.outerHTML}); | |
}); | |
} | |
createInnerHtml() { | |
return { | |
__html: this.state.icon | |
}; | |
} | |
render() { | |
return (<div dangerouslySetInnerHTML={this.createInnerHtml()}> | |
</div>); | |
} | |
} | |
class Widget extends React.Component { | |
render() { | |
return (<SvgImage url='https://cdn.css-tricks.com/wp-content/uploads/2015/05/kiwi.svg'></SvgImage>); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment