Is there a way to dynamically import arrays inside separate files in a directory in Reactjs?

Viewed 27

What I had tried.

I tried importing every file inside the directory using require.context. It did the job and read the files but I am stuck at accessing what's inside the files.

function importAll(r) {
    let imports = {};
    r.keys().map((item, index) => { imports[item.replace('./', '')] = r(item); });
    return imports;
}

const imports = importAll(require.context('../questionsdata', false, /\.(js)$/));

If I get it correctly, That is the equivalent of me importing every single JS file in the directory. Each file contains 1 array that is exported and needs to be accessed.

0 Answers
Related