简体   繁体   English

React JS 中的条件渲染和图像路径

[英]Conditional rendering and image path in React JS

I don't really understand one moment.一瞬间真的搞不懂Let's say I have a component which conditionally renders data like this:假设我有一个组件,它有条件地呈现这样的数据:

<div>
{isLoading ? <img src="../pathname/img.jpg" alt="loading" /> : <Page />}
</div>

Image is not rendered this way, shows a broken file icon.图像未以这种方式呈现,显示损坏的文件图标。 However, if I import the same image it works just fine:但是,如果我导入相同的图像,它就可以正常工作:

import loadingImage from "../pathname/img.jpg"
<div>
{isLoading ? <img src={loadingImage} alt="loading" /> : <Page />}
</div>

I use npm start and my editor is VS Code.我使用npm start ,我的编辑器是 VS Code。 Do you know may be what is the reason to import it and not just simply provide a path name in src even if path is the same?您知道导入它的原因可能是什么,而不仅仅是在src提供一个路径名,即使路径相同? The path I provided and file name was 100% correct in the first example.在第一个示例中,我提供的路径和文件名是 100% 正确的。

That happens because your image probably isn't inside public folder.发生这种情况是因为您的图像可能不在public文件夹中。

You can add assets to the public folder and then you will have access to it.您可以将资产添加到公共文件夹,然后您就可以访问它。

eg For the code below, you will need a pathname folder with the img.jpg inside the public folder.例如,对于下面的代码,您将需要一个pathname文件夹,在public文件夹中包含 img.jpg。

<img src="../pathname/img.jpg" alt="loading" />

But the best way to do this is using import in JavaScript files.但最好的方法是在 JavaScript 文件中使用 import 。 This mechanism provides a number of benefits:这种机制提供了许多好处:

  • Scripts and stylesheets get minified and bundled together to avoid extra network requests.脚本和样式表被缩小并捆绑在一起以避免额外的网络请求。
  • Files are included in the bundle文件包含在捆绑包中
  • Missing files cause compilation errors instead of 404 errors for your users.缺少文件会导致编译错误,而不是用户的 404 错误。
  • Result filenames include content hashes so you don't need to worry about browsers caching their old versions.结果文件名包括内容哈希,因此您无需担心浏览器缓存其旧版本。

With import , when the page is rendered, you will see sometinhg like this in the image: src="/static/media/img.xxxxx.jpg" .使用import ,当页面呈现时,您将在图像中看到类似这样的内容: src="/static/media/img.xxxxx.jpg" Thats because using import tells webpack to include that file in the bundle.那是因为使用 import 告诉 webpack 将该文件包含在包中。

You can check more here - adding-images-fonts-and-files and here using-the-public-folder您可以在此处查看更多信息 - 添加图像字体和文件并在此处使用公共文件夹

You miss the curly brackets.你错过了大括号。 This will work:这将起作用:

<div>
  {isLoading ? <img src={'../pathname/img.jpg'} alt='loading' /> : <Page />}
</div>

If you don't import the image file url then if you're using a webpack type bundler it won't include the file in the final build for you (to save you including unnecessary files that are not used).如果您不import图像文件 url,那么如果您使用的是 webpack 类型的捆绑器,它将不会在最终构建中包含该文件(以节省您包括未使用的不必要文件)。

The best way is to tell the bundler you need the file with an import statement.最好的方法是通过import语句告诉捆绑器您需要该文件。 It will then store the file efficiently and auto update the URL reference as it gets updated in the final build.然后它将有效地存储文件并在最终构建中更新 URL 引用时自动更新它。

You could also put the image in a public folder BUT this has several downsides as noted by other answers here.In one situation I was trying to update the URL string to use in CSS as a background-image: - but at run time, the file structure will have been changed by your web-bundler (but it won't understand it needs to update a string) so your URL reference will point to an incorrect location.您也可以将图像放在公用文件夹中,但这有几个缺点,正如此处其他答案所指出的那样。在一种情况下,我试图更新 URL 字符串以在 CSS 中用作background-image: - 但在运行时,您的网络捆绑程序将更改文件结构(但它不会理解它需要更新字符串),因此您的 URL 引用将指向不正确的位置。

There's some good reading here: https://www.freecodecamp.org/news/react-background-image-tutorial-how-to-set-backgroundimage-with-inline-css-style/这里有一些很好的阅读: https : //www.freecodecamp.org/news/react-background-image-tutorial-how-to-set-backgroundimage-with-inline-css-style/

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

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