Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

react-modal warns that the app element is not defined

ErrorWarning: 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.

  • Applies to react-modal 3.x, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/null
  2. 2Match what you see, then jump to its fix

    1. 1The warning appears in the console when a modal opens, and no setAppElement call existssetAppElement was never calledFix
    2. 2Your 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. 3The 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

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

Did this fix it?

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 opens

Confirmed by reactcommunity.org

Did this fix it?

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').length

2Fix

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

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.