简体   繁体   English

React JS 中图片的动态加载

[英]Dynamic loading of images in React JS

I am trying to dynamically get images from my images folder based on some information retrieved from the database.我正在尝试根据从数据库中检索到的一些信息从我的图像文件夹中动态获取图像。 Gone through as many resources as I could but still unable to solve the problem.我已经尽可能多地使用了资源,但仍然无法解决问题。 Here's my code:这是我的代码:

import scimitar from "../../images/scimitar.png";
import defender from "../../images/defender.png";
import arrows from "../../images/arrows.png";
import cape from "../../images/cape.png";
import platebody from "../../images/platebody.png";

const ItemCard = ({ item }) => {


    return (
        <div>
            <p key={item.id}>ID: {item.id}</p>
            <p>Name: {item.name}</p>
            <p>{item.examine}</p>
            <p>
                <Link to={`/items/${item.id}`}>{item.name}</Link>
            </p>

            <img src={require(item.name)} alt={item.examine} />
        </div>
    )
}

const ItemList = () => {
    const [items, setItems] = useState(null);

    const populateItems = async () => {
        const data = await getItems();
        setItems(data);
    };

    useEffect(() => populateItems(), []);


    return (
        <div>
            {items &&
                items.map((item, index) => (
                    <ItemCard item={item} key={index} />
                ))
            }
        </div>
    )
}
        <img src={item.name} alt={item.examine} />

It looks like there are a couple of issues going on.看起来有几个问题正在发生。 Using template literals like使用模板文字,如

<img src={`../../images/${item.name}.png`} alt={item.examine} />

won't work either.也不会工作。 The reason why is src doesn't taken in a path to picture, it looks at a url your website uses. src没有在图片路径中获取的原因,它查看您的网站使用的 url。 You'll need to setup your React app to serve public images (eg make sure something like localhost:1337/images/schimitar.png works).你需要设置你的 React 应用程序来提供公共图像(例如确保像localhost:1337/images/schimitar.png有效)。

Only then can you reference it using只有这样你才能引用它使用

<img src={`/images/${item.name}.png` />

To serve static files in create-react-app check out this link.要在 create-react-app 中提供静态文件,请查看此链接。 If you have another setup you'll need to use something like babel-plugin-file-loader to serve public assets.如果您有其他设置,则需要使用babel-plugin-file-loader来提供公共资产。

Not sure why this worked but I placed the path of the image in a variable before passing it to the src path of the image tag.不知道为什么会这样,但我将图像的路径放在一个变量中,然后再将它传递给图像标签的 src 路径。

const ItemCard = ({ item }) => {
const imageItem = `/images/${item.name}.png`;

return (
    <div>
        <p key={item.id}>ID: {item.id}</p>
        <p>Name: {item.name}</p>
        <p>{item.examine}</p>
        <span>Quantity: {item.quantity}</span>
        <p>
            <Link to={`/items/${item.id}`}>{item.name}</Link>
        </p>
        <img src={imageItem} alt={item.examine} />

    </div>
  )
}
export default ItemCard;

Try the following code if you are trying to get the image from a static path.如果您尝试从静态路径获取图像,请尝试以下代码。

import image1 from 'images/image1.png';

<img src={image1} alt="image1" />

If you are trying to dynamically add the image then try the following code,如果您尝试动态添加图像,请尝试以下代码,

const imgName = "image1.png"

return (
    <div>
         { imgName && <img src={`images/${imgName}`} alt="imgName" /> }
    </div>
)

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

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