Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a JSON file in react.js

I have to read JSON file using React.js which is stored in local drive in Mac. I tried using fetch() api but I get data undefined. Heres my code.

fetch('http://localhost:300/107k.json')
    .then(req => console.log(req))
    .then(data => console.log(data))

I am getting data as undefined. How will I be able to read JSON file in react.js?

like image 398
Akash Sateesh Avatar asked Dec 02 '22 12:12

Akash Sateesh


1 Answers

Just use import instead

import react from 'react'

//... rest of imports

import MyJson from '../path/to/json/107k.json';
like image 148
Mμ. Avatar answered Dec 06 '22 10:12

Mμ.