Home > Back-end >  How I can show data from local json file using axios in React Native?
How I can show data from local json file using axios in React Native?

Time:11-20

I'm trying to make some mockup data and call it. since my backend is not completed yet .

CodePudding user response:

I believe reading a local file with Axios is a wrong approach.

Check this link: https://dev.to/buaiscia/mock-an-axios-call-with-a-json-file-1j49

Instead, I recommend using FileSystem!

You can read local data in javascript using FileSystem. Steps:

  1. Install fs

    npm install fs
    
  2. Import fs in file

    var fs = require('fs')
    
  3. Then read the file:

    fs.readFileSync('fileName.json');
    

CodePudding user response:

You could serve the JSON locally. You could run something like json-server in the background, say on port 5000, and make your requests to http://<your computer's ip>:5000.

Note: don't use http://localhost:5000 - this will only work on the iOS simulator, since it's the only "device" that shares your local machine's network config. Get the IP address of your computer and use that instead.

  • Related