what is the best practice from the following in using json data from data.json file in reactjs

Viewed 57

what is the best practice from the following in using json data from data.json file in reactjs web application?:-

  1. Storing json data file in the same file system and import it in react component.
  2. run json file on local server and fetch it.
  3. use backend server.

and which option will work fine after deploying the application? and why?

1 Answers

It really depends on the use case. If the data.json is static, importing it will include the file in your bundle and will create a better user experience since you will not have to make an HTTP call and wait for its response.

fetching a file from your local server will give you a smaller bundle size (quicker initial load) and while you fetch the file (should be quicker than backend since it is local) you can show some loading animation.

If your data file is dynamic you may need to serve it from the backend.

If you are server-side-rendering your page, importing it will include the data in your HTML source which can be an SEO advantage.

For me, if it a small enough static file I would Import it (option 1)

Related