I have been importing data in the form of a JSON file into a React component:
import data from '../../public/json/data.json';
I am using the create-react-app tool, and when I run npm run build, as expected the data.json file is bundled as part of the build/static/main.js file.
What I would like is for this data.json file not to be bundled but to be referenced at run-time each time the component runs. I.e. so that it can be edited in the built app and for these edits to show in the app. I thought that by including it in my public (as opposed to src) folder this would work, but of course I am still importing it so it is bundled. Is there a way to reference it without bundling it?
Use the fetch API to retrieve the JSON file asynchronously.
Check out https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch for example implementations.