ESLint warns that img elements must have an alt prop in React
img elements must have an alt prop, either with meaningful text, or an empty string for decorative imagesShort answer
The jsx-a11y/alt-text rule found an img without a usable alt. Give meaningful text when the image carries information, and an empty string alt="" when it is decorative. A role of presentation does not replace alt, and props spread onto an img must pass alt explicitly.
Fix it fast
1Find out which cause you have
Run ESLint on the file to see the exact line, then find every img in the project that has no alt.
npx eslint src2Match what you see, then jump to its fix
- 1
The warning points at a plain <img src=... /> lineThe img has no alt at allFix - 2
The img has role="presentation" or role="none" and still warnsrole="presentation" was used instead of altFix - 3
The line is <img {...props} /> and the alt is passed by the callerProps are spread onto the img and alt is hidden inside themFix - 4
The warning is on <Image /> although there is no img in that fileA wrapper component such as Image is checked through the rule's img optionFix
- 1
What does the error look like?
img elements must have an alt prop, either with meaningful text, or an empty string for decorative imagesjsx-a11y/alt-text
Cause 1 of 4
The img has no alt at all
1Check
Look at the tag. The rule fails an alt that is missing, written without a value, or set to undefined.
grep -rn "<img" src/2Fix
Describe the image if it adds information. If it is decoration, or an icon next to a text label, use an empty string.
<img src={logo} alt="Acme logo" />
<img src={divider} alt="" />3Verify
Run ESLint again on the file.
npx eslint src/App.jsxConfirmed by github.com
Cause 2 of 4
role="presentation" was used instead of alt
1Check
The rule documentation lists these roles on an image as a failing case; plain HTML already has the answer.
grep -rnE "role=.(presentation|none)" src/2Fix
Remove the role and use alt="".
// before
<img src={bg} role="presentation" />
// after
<img src={bg} alt="" />3Verify
ESLint reports no alt-text error for that file.
npx eslint src/Hero.jsxConfirmed by github.com
Cause 3 of 4
Props are spread onto the img and alt is hidden inside them
1Check
The rule treats a spread without an explicit alt as failing, even if the caller does pass one.
grep -rn "<img {" src/2Fix
Destructure alt and pass it on explicitly.
function Photo({ alt, ...props }) {
return <img alt={alt} {...props} />;
}3Verify
ESLint passes, and callers still set alt on Photo.
npx eslint src/Photo.jsxConfirmed by github.com
Cause 4 of 4
A wrapper component such as Image is checked through the rule's img option
1Check
The rule can map the img element to custom component names, so a wrapper listed in the config is checked like an img.
grep -rn "alt-text" eslint.config.* .eslintrc* package.json 2>/dev/null2Fix
Give the wrapper an alt prop the same way. If a component should not be checked, remove it from the option list in your config.
<Image src={hero} alt="Product dashboard" />3Verify
Run ESLint on the file.
npx eslint srcConfirmed by github.com
How do I stop it coming back?
- Treat alt as part of the image component's required props in TypeScript.
- Keep jsx-a11y in the recommended config so new images are checked at save time.
- Write alt text for what the image says, not 'image of'.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix