在React项目中出现这个红色warning:
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}`.
解决办法:
... import ReactModal from ‘react-modal‘; ... class App extends Component {
componentWillMount() {
//办法2:
ReactModal.setAppElement(document.body); //setAppElement()函数是静态的,一个class设置一遍即可
}
render(){ ... <ReactModal
//办法之1:
appElement={document.body} className="modal" overlayClassName="overlay" isOpen={foodModalOpen} onRequestClose={this.closeFoodModal} contentLabel="Modal" > ... } }
在本人的实际项目中:父模块和子模块使用的package.json依赖包都是相同的,父模块必须引入子模块的react-modals,这样才能整个项目中使用统一的一个class。
项目全局搜索:
WrappedGui.setAppElement = ReactModal.setAppElement;