Open bebraw opened 9 years ago
I ended up with
var React = require('react');
var hljs = require('highlight.js');
var CodeComponent = React.createClass({
componentWillMount: function () {
this.highlight();
},
componentDidUpdate: function () {
this.highlight();
},
highlight: function () {
var language = this.props.language;
var code = this.props.code;
this.setState({
highlightedCode: hljs.highlight(language, code).value
});
},
render: function () {
var highlightedCode = this.state.highlightedCode || '';
return React.createElement('pre', {key: this.props.key},
React.createElement('code', {
dangerouslySetInnerHTML: {__html: highlightedCode}
})
);
}
});
module.exports = CodeComponent;
There's possibly a nicer way to deal with it. dangerouslySetInnerHTML
is a little meh always.
You want to create a pull on this? It is at least a step forward :-)
It returns just
code
element. By the looks of it,CodeComponent
does juston
componentDidMount
andcomponentDidUpdate
.There has to be some better way to deal with this.