I'm trying to embed a Gist using ReactJS, but I'm getting the following error:
Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
Here's my component:
var EmbeddedGist = React.createClass({
render: function() {
return (
<div id="gist-container" />
);
},
componentDidMount: function() {
var src = this.props.srcUrl + ".js";
$('#gist-container').html('<script src="' + src + '"></script>');
}
});
I'm calling it from within another component, as such:
<EmbeddedGist srcUrl="https://gist.github.com/awalGarg/a5bd02978cecf3703f61" />
Any ideas on how to make this work?
I implemented it with updating of the props using componentWillRecieveProps like binaryMuse suggested.
Did something like this:
Where _getGistData is simply the original code that was in componentDidMount
The Gist embed script uses
document.write
to write the HTML that makes up the embedded Gist into the document's HTML. However, by the time your React component adds thescript
tag, it's too late to write to the document.While you can't add the Gist embed script to your page dynamically, you can get the contents of the Gist via JSONP and write that into the document yourself. Here's a component that takes a
gist
property and an optionalfile
property and renders the gist for you.You can use it like this:
Take a look at this example of it working on JSfiddle: http://jsfiddle.net/BinaryMuse/nrb6zxfw/
One way to improve this would to ensure that an existing
EmbeddedGist
component that has itsgist
orfile
prop change would update to use the new data by hooking intocomponentWillReceiveProps
.