简体   繁体   English

在 React 的文件夹中循环所有图像的好方法是什么?

[英]What would be a good way to loop all images in a folder in React?

I have some images in a folder and I try to loop it.我在文件夹中有一些图像,我尝试循环播放它。 For example, a folder named "coupons" has some images like "coupon1.png","coupon2.png","coupon3.png".... Then in one component, I try to make a function to import all the images and return例如,一个名为“coupons”的文件夹有一些图像,如“coupon1.png”、“coupon2.png”、“coupon3.png”...。然后在一个组件中,我尝试创建一个函数来导入所有图像并返回

<img src={coupon1} alt='coupon1' className="slide" />
<img src={coupon2} alt='coupon2' className="slide" />
<img src={coupon3} alt='coupon3' className="slide" />
.....

May I know what would be a good way to do this?我可以知道什么是这样做的好方法吗? How to avoid import images one by one?如何避免一张一张导入图片? And how to get the total number of the images files in the folder?以及如何获取文件夹中图像文件的总数?

import coupon1 from '../assets/coupons/coupon1.png';
import coupon2 from '../assets/coupons/coupon2.png';
import coupon3 from '../assets/coupons/coupon3.png';
...

And how to loop them?以及如何循环它们? I tried to use template string but it ends with string not variable so still not work.我尝试使用模板字符串,但它以字符串而不是变量结尾,所以仍然不起作用。 Thank you so much!太感谢了!

You have 3 options:您有 3 个选项:

1) You can use webpack and generate alias with file list. 1)您可以使用 webpack 并使用文件列表生成别名。

webpack.config.js : webpack.config.js

const webpack = require('webpack');
const child_process = require('child_process');
let stdout = child_process.execSync("find . -regex  '.*\\(png\\|jgeg\\)$'", {encoding: 'utf8'});
let files = stdout.split('\n');


module.exports = {
    plugins = [
        new webpack.DefinePlugin({
            IMAGES: JSON.stringify(files),
        }),
    ];
} 

index.js : index.js

Promise.all(
    files.map(a => import(a))
).then(
    filesSources => console.log(`DO something with array: ${filesSources}`)
);

2) You can require images with context: 2)您可以要求带有上下文的图像:

index.js : index.js

list = require.context('./', false, /\.(png|jpeg)$/);

3) You can use copyWebpackPLugin if you don't need to work with sources: 3)如果您不需要使用源代码,则可以使用 copyWebpackPLugin:

webpack.config.js : webpack.config.js

const CopyWebpackPlugin = require('copy-webpack-plugin');
new CopyWebpackPlugin([
  {from: './src/assets/images', to: 'images'},
 ]),

Note in 3) case image won't be processed by webpack loaders chain.注意 3) 案例图像不会被 webpack 加载器链处理。 In first 2 cases you need to add rule for FileLoader.在前两种情况下,您需要为 FileLoader 添加规则。 You can find example of 3 cases at https://github.com/akoidan/pychat/tree/master/fe您可以在https://github.com/akoidan/pychat/tree/master/fe找到 3 个案例的示例

Oh and I just found a way like this:哦,我刚刚找到了这样的方法:

const slides = Array(6).fill().map((item, i) => (
            <img key={i} src={require(`../assets/coupons/coupon${i + 1}.png`)} className="slide" />
        ));

but I'm not sure is this a good way to do this?但我不确定这是一个好方法吗? And what is this syntax of using require inside jsx?在 jsx 中使用 require 的语法是什么? May I know is there any explanation about why using require here?我可以知道为什么在这里使用 require 有任何解释吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM