Skip to content

Instantly share code, notes, and snippets.

@mikkel
Last active September 12, 2019 05:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikkel/8b79a713ff06bbec379d to your computer and use it in GitHub Desktop.
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.
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