Last active
June 17, 2018 06:44
-
-
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
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
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