oegeonu
oegeonu

Reputation: 1

React component that reads from a .txt file and displays data

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

Answers (2)

Kyle Calica-St
Kyle Calica-St

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

Thomas Aumaitre
Thomas Aumaitre

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

Related Questions