Access JSON file in React without importing it

I imported the data as a file JSONinto the React component:

import data from '../../public/json/data.json';

I use the create-react-app tool , and when I run npm run build, as expected, the file is data.jsoninserted as part of the file build/static/main.js.

I would like this file data.jsonnot to be linked, but for a link at runtime every time the component is executed. That is, so that it can be edited in the embedded application for these changes, which will be shown in the application. I thought that by including it in a folder public(as opposed to src), this would work, but of course I am still importing, therefore it is connected. Is there any way to refer to it without binding it?

+4
source share
1 answer

Use the fetch API to retrieve the JSON file asynchronously.

Check https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch , for example, implementations.

0
source

Source: https://habr.com/ru/post/1668429/


All Articles