Reputation: 1
I am trying to create a react component that reads from a .txt file with in my project and displays the text. Not sure where to start. I know how to call the component but not code for the component.
Upvotes: 0
Views: 86
Reputation: 2933
If you are just trying to get values to pass into your React component then you can try turning it into a JSON file and then
import data from '<path_to_json_file>';
Upvotes: 0
Reputation: 807
You must use a Node.js default module called fs
https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback
const fs = require("fs");
fs.readFile('myfile.txt', (err, data) => {
if (err) throw err;
// Do your stuff here
console.log(data);
});
Upvotes: 2