This is a react project. I have a folder name data inside src where there are multiples JSON files. Now I need to fetch all the JSON files from data directory but the problem is I don't know the names of the JSON files so how can I fetch all of the JSON files from data folder in React?
You can also use "Promise.all" in order to await every single file as shown below. Use the method suggested by the answer above, and implement the code below as well in order to get the best result!
Promise.all([promise1, promise2, promise3]).then((values) => {
console.log(values);
});
If you want to iterate through them you should create an index.ts file in your JSONs dir, and import all files in it, and export all of them as a single array that contains all the JSONs, and import it anywhere and do what you want.
index.js:
import file1 from './file1.json';
import file2 from './file2.json';
let items = [file1, file2];
export default items;
anywhere else in your app:
import items from 'URLS_DIR';
console.log(items, ` <--- items ----`); // iterate through them