简体   繁体   中英

How to get all files in a directory?

I have a number of files in a folder in React. Each of them must be self-initialized. For this, I need to import them. I wouldn't want to import each file individually. I need to get a list of all the files. But we do not have access to FS on the client. I tried this https://github.com/diegohaz/list-react-files solution, but it does not work (it looks like it somehow uses fs, and gives an error). Can I solve my problem in a straightforward way? Can this be done using the web-pack? Any ideas guys?

Yes, we don't have fs on the client so we need to each file manually. but there is an easy way of doing it.

Make a registry file where you put the path of file from registry.js relative

{
  "file1": "/folder1/sda.png",
  "file2": "/folder2/asd.png"
}

Now you don't need to import each file manually import registry.json

import registry from registry.json

for x in registry {
  try {
     require(`path to registry/${registry[x]}`)
  } catch (e) {
    console.log(e)
 }

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