简体   繁体   中英

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

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM