“Warning: react-modal: App element is not defined. Please use `Modal.setAppElement(el)` or set `appElement={el}`”

后端 未结 12 1259
轻奢々
轻奢々 2021-02-07 03:36

How to fix this warning in console of a React app using the react-modal package:

Warning: react-modal: App element is not defined. Please use Moda

12条回答
  •  抹茶落季
    2021-02-07 04:15

    For reference, since it was a pain for me, if you are doing SSR, use the following code to prevent errors server-side:

    if (typeof(window) !== 'undefined') {
        ReactModal.setAppElement('body')
    }
    

    You could put this in componentDidMount() anywhere you use a modal or I put it in a custom modal component so it's nice and DRY.

提交回复
热议问题