Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save onofapolivio/2b11fc3b14438290fafee052763fa28e to your computer and use it in GitHub Desktop.
Save onofapolivio/2b11fc3b14438290fafee052763fa28e to your computer and use it in GitHub Desktop.
A React component to read text file.
import React from "react";
/*
Read a text file and out put the content.
Example Usage:
var myTxt = require("./myTxt.txt");
...
<TextFileReader
txt={myTxt}
/>
*/
class TextFileReader extends React.Component {
constructor(props) {
super(props);
this.state = {
text: ""
};
}
componentDidMount() {
this.readTextFile(this.props.txt);
}
readTextFile = file => {
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = () => {
if (rawFile.readyState === 4) {
if (rawFile.status === 200 || rawFile.status == 0) {
var allText = rawFile.responseText;
this.setState({
text: allText
});
}
}
};
rawFile.send(null);
};
render() {
return (
<div>
{this.state.text.split("\n").map((item, key) => {
return <span key={key}>{item}<br /></span>;
})}
</div>
);
}
}
export default TextFileReader;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment