question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setAppElement from react-modal

See original GitHub issue

Hi everybody,

I have integrated react-image-lightbox however I have the following error message: “Uncaught Error: react-modal: You must set an element with Modal.setAppElement(el) to make this accessible”

Anyone know how could I fix it? I am using “react-modal”: “^1.3.0” dependency.

Thanks a lot for your help.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
malroccommented, May 4, 2017

Same issue. This is a known bug in react-modal: https://github.com/reactjs/react-modal/issues/133 The workaround helped, but since react-image-lightbox relies on react-modal and calls it implicitly, it isn’t just a “react-modal usage issue”, it is a react-image-lightbox issue too.

2reactions
kumekaycommented, Nov 25, 2016

I experienced this issue using webpack. More details on this issue https://github.com/reactjs/react-modal/issues/133

I worked around it by adding

import Modal from 'react-modal';
   componentWillMount() {
        Modal.setAppElement('body');
    }

Full example

import React, {Component} from 'react';
import Lightbox from 'react-image-lightbox';
import Modal from 'react-modal';

const images = [
    '//placekitten.com/1500/500',
    '//placekitten.com/4000/3000',
    '//placekitten.com/800/1200',
    '//placekitten.com/1500/1500'
];

export default class Gallery extends Component {
    constructor(props) {
        super(props);

        this.state = {
            photoIndex: 0,
            isOpen: false
        };
    }

    componentWillMount() {
        Modal.setAppElement('body');
    }

    render() {
        const {
            photoIndex,
            isOpen
        } = this.state;

        return (
            <div>
                <button
                    type="button"
                    onClick={() => this.setState({isOpen: true})}
                >
                    Open Lightbox
                </button>

                {isOpen &&
                <Lightbox
                    mainSrc={images[photoIndex]}
                    nextSrc={images[(photoIndex + 1) % images.length]}
                    prevSrc={images[(photoIndex + images.length - 1) % images.length]}

                    onCloseRequest={() => this.setState({isOpen: false})}
                    onMovePrevRequest={() => this.setState({
                        photoIndex: (photoIndex + images.length - 1) % images.length,
                    })}
                    onMoveNextRequest={() => this.setState({
                        photoIndex: (photoIndex + 1) % images.length,
                    })}
                />
                }
            </div>
        );
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

setAppElement - react-modal documentation
This example shows how to use setAppElement to properly hide your application from screenreaders and other assistive technologies while the modal is open....
Read more >
"Warning: react-modal: App element is not defined. Please ...
Some solutions are given in react-modal issue #133: ... setAppElement() is called with null or not called at all with the <script />...
Read more >
Error: You must set an element with `Modal.setAppElement(el ...
react -modal was trying to set the app element to document.body , while it doesn't exist yet. If you did the same, within...
Read more >
React Modal - Using setAppElement - CodePen
1. ReactModal.setAppElement('#main'); ; 2. ​ ; 3. class ExampleApp extends React.Component { ; 4. constructor () { ; 5. super();.
Read more >
[Resolved] Warning: react-modal: App element is not defined ...
setAppElement (el)` or set `appElement={el}`. November 13, 2022. When using the react-modal package component, we see a Warning message in the console saying ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found