Skip to content

Instantly share code, notes, and snippets.

@icyJoseph
Last active June 17, 2018 06:44
Show Gist options
  • Save icyJoseph/613af65bd1b66621d6292b18898e5176 to your computer and use it in GitHub Desktop.
Save icyJoseph/613af65bd1b66621d6292b18898e5176 to your computer and use it in GitHub Desktop.
React Component to read its own source code, demo at: https://codesandbox.io/s/kmlmzlvn5v
import React, { Component } from "react";
import ReactDOM from "react-dom";
class App extends Component {
constructor(props) {
super(props);
this.state = {
result: ""
};
}
componentDidMount() {
const blob = new Blob([App]);
const reader = new FileReader();
reader.addEventListener("loadend", () =>
this.setState(() => {
const { result } = reader;
return { result };
})
);
reader.readAsText(blob);
}
render() {
const { result } = this.state;
return <div>{result}</div>;
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment