Skip to content

Instantly share code, notes, and snippets.

@anmolsukki
Created September 13, 2020 08:56
Show Gist options
  • Save anmolsukki/4aa87b14b7baad03440444068363d0e2 to your computer and use it in GitHub Desktop.
Save anmolsukki/4aa87b14b7baad03440444068363d0e2 to your computer and use it in GitHub Desktop.
[ React JS ] Render text file or access direct txt file

we can render text file directly

create FileName.txt in public folder

and run localhost:3000/FileName.txt
Anmol Kumar Singh
import React, { Component } from 'react';
import MyText from './FileName.txt';
class RenderText extends Component {
componentDidMount = () => {
this.handleClick();
};
handleClick = () => {
fetch(MyText)
.then((r) => r.text())
.then((text) => {
console.log(text);
this.setState({ text });
});
};
render() {
return (
<div>
<p>{this.state?.text}</p>
</div>
);
}
}
export default RenderText;
@anmolsukki
Copy link
Author

make public

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment