Why is React.js removing the srcset tag on <img />?

牧云@^-^@ 提交于 2019-12-03 23:20:56

问题


When I have the srcset property on my <img /> tag, why doesn't it show up in the browser? It appears as through React.js is stripping it out.

<img src="/images/logo.png" srcset="/images/logo-1.5x.png 1.5x, /images/logo-2x.png 2x" />

回答1:


The solution is to use srcSet instead of srcset.

<img src="/images/logo.png" srcSet="/images/logo-1.5x.png 1.5x, /images/logo-2x.png 2x" />

Reference: https://facebook.github.io/react/docs/tags-and-attributes.html under HTML Attributes




回答2:


Another ugly solution using template literals:

<img
  alt=''
  src={require('../../assets/images/logo/logo.png')}
  srcSet={`
    ${require('../../assets/images/logo/logo@2x.png')} 2x, 
    ${require('../../assets/images/logo/logo@3x.png')} 3x
  `}
/>


来源:https://stackoverflow.com/questions/34695899/why-is-react-js-removing-the-srcset-tag-on-img

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!