“冗余 alt 属性屏幕阅读器已经将 `img` 标签宣布为图像”代码错误

问题描述

我在 React js 文件中输入了 <img src= {'https://robohash.org/${id}?size=300x300'}alt="photo" />, 在我保存文件并在 GitBash 上收到一条错误消息后,

Redundant alt attribute. Screen-readers already announce 'img' tags as an image. You don’t need to use the words 'image','photo,' or 'picture' (or any specified custom words) in the alt prop jsx-a11y/img-redundant-alt

然后我从代码删除alt,然后我收到了错误消息:

img elements must have an alt prop,either with meaningful text,or an empty string for decorative images jsx-a11y/alt-text

我该如何解决这个问题?

解决方法

我试过这个,它奏效了:

<input type="image" img src = {'https://robohash.org/${id}?size=300x300'} alt="photo" />

(我使用了 `` 而不是 url 的单引号)

,

'something' 与单词 'photo' 和 'image'(或任何指定的自定义单词)不同。