"警告:react-modal:App 元素未定义.请使用`Modal.setAppElement(el)`或设置`appElement={el}`"; [英] "Warning: react-modal: App element is not defined. Please use `Modal.setAppElement(el)` or set `appElement={el}`"

查看:90
本文介绍了"警告:react-modal:App 元素未定义.请使用`Modal.setAppElement(el)`或设置`appElement={el}`";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 react-modal 包在 React 应用程序的控制台中修复此警​​告:

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

警告:react-modal:App 元素未定义.请使用 Modal.setAppElement(el) 或设置 appElement={el}

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

我没有成功弄清楚 el 应该是什么.

I have not been successful at figuring out what el is supposed to be.

上下文:在我的 App.js 根组件文件中:

Context: in my App.js root component file:

...
import Modal from 'react-modal';
...
class App extends Component {
  ...
  render(){
    ...  
    <Modal
      className="modal"
      overlayClassName="overlay"
      isOpen={foodModalOpen}
      onRequestClose={this.closeFoodModal}
      contentLabel="Modal"
    >
    ...
  }
}

其中 ... 表示代码未显示.

Where ... indicates code not shown.

一切正常,但打开 Modal 时,控制台中出现以下警告:

Everything works fine, but when the Modal is opened, the following Warning appears in my console:

index.js:2177 警告:react-modal:App 元素未定义.请使用 Modal.setAppElement(el) 或设置 appElement={el}.这是必需的,因此屏幕阅读器在打开模态时看不到主要内容.不建议这样做,但您可以通过设置 ariaHideApp={false} 选择退出.

index.js:2177 Warning: react-modal: App element is not defined. Please use Modal.setAppElement(el) or set appElement={el}. This is needed so screen readers don't see main content when modal is opened. It is not recommended, but you can opt-out by setting ariaHideApp={false}.

react-modal 文档 中,我只能找到以下:

In the react-modal docs all I can find is the following:

应用元素app 元素允许您指定应隐藏的应用程序部分(通过 aria-hidden),以防止屏幕阅读器等辅助技术读取模态内容之外的内容.

App Element The app element allows you to specify the portion of your app that should be hidden (via aria-hidden) to prevent assistive technologies such as screenreaders from reading content outside of the content of your modal.

如果你在做服务器端渲染,你应该使用这个属性.

If you are doing server-side rendering, you should use this property.

可以通过以下方式指定:

It can be specified in the following ways:

DOM元素
Modal.setAppElement(appElement);
查询选择器 - 使用传入类时找到的第一个元素.
Modal.setAppElement('#your-app-element');

不幸的是,这没有帮助!我不知道 el 应该代表什么.

Unfortunately, this has not helped ! I cannot figure out what el is supposed to represent.

以下是我尝试添加到我的 Modal 组件中的许多属性变体中的一些:

Here are some of the many property variations I have tried adding to my Modal component:

`appElement={el}`,  
`appElement="root"` where `root` is the id that my App component is injected into   
`appElement={'root'}`   
`appElement="div"`,   
`appElement={<div>}`,   
`appElement={"div"}`  
}`,`appElement="div"}`

I've also tried calling Modal.setAppElement('root'); from inside src/index.js, where root is the root element that my App component is injected into, and index.js is where I do that.

我也试过从 src/index.js 内部调用 Modal.setAppElement('root');,其中 root 是我的 App 组件被注入的根元素,而 index.js 就是我这样做的地方.

解决方案

推荐答案

反应模式问题 #133:

问题出在这里:取决于它何时评估 react-modal@1.6.5:/lib/helpers/ariaAppHider.js#L1:

  • document.body does not exist yet and it will resolve to undefined || null.
  • if Modal.setAppElement() is called with null or not called at all with the <script /> placed on <head /> (same as above).
  • Probably it can also happen if called with a selector that does not match any results.
  • document.body 尚不存在,它将解析为 undefined ||空.
  • 如果 Modal.setAppElement() 使用 null 调用或根本不调用
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆