react-modal warns that the app element is not defined
Warning: react-modal: App element is not defined. Please use `Modal.setAppElement(el)` or set `appElement={el}`Short answer
react-modal needs to know which part of your page to hide from screen readers while the modal is open. Call Modal.setAppElement('#root') once, before any modal opens, with the selector or element that holds your app. Use ariaHideApp={false} only if you already hide that content another way.
Fix it fast
1Find out which cause you have
Find where your app mounts and which element id it uses. That element is what setAppElement needs.
grep -n "createRoot" src/*.jsx src/*.tsx 2>/dev/null grep -n "id=\"root\"" index.html public/index.html 2>/dev/null2Match what you see, then jump to its fix
- 1
The warning appears in the console when a modal opens, and no setAppElement call existssetAppElement was never calledFix - 2
Your layout already marks the background content aria-hidden or inert when a dialog opensYou hide the app yourself and the warning is just noiseFix - 3
The warning is gone, but the background is not hidden after you re-render parts of the pageThe hidden elements went stale after the DOM changedFix
- 1
What does the error look like?
Warning: react-modal: App element is not defined. Please use `Modal.setAppElement(el)` or set `appElement={el}`
Cause 1 of 3
setAppElement was never called
1Check
react-modal hides the app behind the modal by setting aria-hidden on it, so screen readers do not read the page under the dialog. It cannot guess which element that is.
grep -rn "setAppElement" src/2Fix
Call it once, somewhere that runs before any modal opens, such as the entry file.
import Modal from 'react-modal';
Modal.setAppElement('#root');3Verify
Open a modal and check the console. The warning is gone, and the root element gets aria-hidden while the modal is open.
// in the browser console, with a modal open
document.getElementById('root').getAttribute('aria-hidden')Confirmed by reactcommunity.org
Cause 2 of 3
You hide the app yourself and the warning is just noise
1Check
If another part of your code already applies aria-hidden to the app content, react-modal does not need to do it a second time.
grep -rn "aria-hidden\|inert" src/2Fix
Pass ariaHideApp={false} on that modal so react-modal skips the step and stops warning.
<Modal isOpen={open} ariaHideApp={false} onRequestClose={close}>
...
</Modal>3Verify
Open the modal; no warning appears.
// browser console should stay clean when the modal opensConfirmed by reactcommunity.org
Cause 3 of 3
The hidden elements went stale after the DOM changed
1Check
When setAppElement receives a selector that matches several elements, or a list of elements, the list is not updated when those elements are removed from the DOM.
// in the browser console
document.querySelectorAll('#root, .app-shell').length2Fix
Call Modal.setAppElement again after such a change, or pass a live HTMLCollection.
Modal.setAppElement(document.getElementsByClassName('app-shell'));3Verify
Open a modal after the page change and check that the elements carry aria-hidden.
// in the browser console, with a modal open
document.querySelectorAll('[aria-hidden="true"]')Confirmed by reactcommunity.org
How do I stop it coming back?
- Put Modal.setAppElement next to the createRoot call so every modal inherits it.
- Keep one root element id for the app and reuse it in the call.
- Open each modal once with a keyboard and a screen reader before release.
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