I get "src="[object Object]"" on <img> when i load image with React.lazy on my web app

huangapple go评论60阅读模式
英文:

I get "src="[object Object]"" on <img> when i load image with React.lazy on my web app

问题

我刚开始学习React JS来制作我的第一个Web应用,我遇到了这个问题:当我在我的const Image上使用React.lazy函数时,图片没有加载。

我已经搜索了很多,但不知道为什么这不起作用。如果你能帮助我,请:/。

import React, { Suspense } from 'react';
import '../style/App.css';

function App() {
    const Image = React.lazy(() => import('../img/macbook.png'));

    return (
        <Suspense fallback={<div>Chargement...</div>}>
            <header>
                <div className="container">
                    <div className="items-left">
                        <h1>Lorem ipsum</h1>
                        <div>Lorem ipsum ipsum ipsum ipsum ipsum ipsum ipsum ipsum ipsum ipsum ipsum </div>
                        <button>Se connecter</button>
                        <button>En savoir plus</button>
                    </div>
                    <div className="items-right">
                        <img src={Image} height="10px" width="10" alt=""/>
                    </div>
                </div>
            </header>
            <footer></footer>
        </Suspense>
    );
}

export default App;

我从React中导入了Suspense,就像在文档中一样。我创建了一个const,在其中执行了React.lazy函数,导入了我的图像的目录。

我在很多论坛上搜索了解决方案,但没有什么有效的(可能是因为我不太明白)。我将该函数放在我的function App() {...} 中。我也复制粘贴了文档示例,并将其集成到我想要的内容中。

英文:

I just started learning React JS to do my first web app and I have this problem: When I do a React.lazy function on my const Image, not image loading.

I have search and I don't have any idea why this isn't working. If you can help me please. :/

import React, { Suspense } from &#39;react&#39;;
import &#39;../style/App.css&#39;;

function App() {

    const Image = React.lazy(() =&gt; import(&#39;../img/macbook.png&#39;));

  return (
    &lt;Suspense fallback={&lt;div&gt;Chargement...&lt;/div&gt;}&gt;
        &lt;header&gt;
            &lt;div className=&quot;container&quot;&gt;
                &lt;div className=&quot;items-left&quot;&gt;
                    &lt;h1&gt;Lorem ipsum&lt;/h1&gt;
                    &lt;div&gt;Lorem ipsum ipsum ipsum ipsum ipsum ipsum ipsum ipsum ipsum ipsum ipsum &lt;/div&gt;
                    &lt;button&gt;Se connecter&lt;/button&gt;
                    &lt;button&gt;En savoir plus&lt;/button&gt;
                &lt;/div&gt;
                &lt;div className=&quot;items-right&quot;&gt;
                    &lt;img src={Image} height=&quot;10px&quot; width=&quot;10&quot; alt=&quot;&quot;/&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/header&gt;
        &lt;footer&gt;

        &lt;/footer&gt;
    &lt;/Suspense&gt;
  );
}

export default App;

I import Suspense from React like on the doc. I do a const where I execute the React.lazy function with the import and the directory of my image.

I search on lot of forum but nothing work (probably because I don't understand).
Put the function on my function App() {...}.
Copy and paste the documentation example and integrate with what i want.

答案1

得分: 1

React.lazy() 用于加载 React 组件,要想延迟加载图片,请将 loading="lazy" 属性添加到你的 <img> 标签中。

<img src='../img/macbook.png' height="10px" width="10" alt="" loading="lazy"/>
英文:

React.lazy() is for loading react components, to lazy load images add the loading=&quot;lazy&quot; attribute to your img

&lt;img src=&#39;../img/macbook.png&#39; height=&quot;10px&quot; width=&quot;10&quot; alt=&quot;&quot; loading=&quot;lazy&quot;/&gt;

huangapple
  • 本文由 发表于 2023年2月14日 18:57:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定