reactjs - 迁移到 material-ui v4,我收到 Modal 组件的警告

标签 reactjs material-ui

自升级 material-uiv4 ,我收到了一些关于 Modal 组件的警告。

例如

Failed prop type: Invalid prop children supplied to ForwardRef(Modal) .

Function components cannot be given refs.

这是模态代码(警告 1):

import React from 'react';
import Proptypes from 'prop-types';
...

class DetailDialog extends React.Component {
  render() {
    const { classes, open, onClose } = this.props;

    return (
      <Dialog
        open={open}
      >
        ...
      </Dialog>
    );
  }
}

DetailDialog.propTypes = {
  open: Proptypes.bool,
  onClose: Proptypes.func,
};

export default DetailDialog;

这是模态代码(警告 2):
import React from 'react';
import PropTypes from 'prop-types';
...

class SelectCatDialog extends React.Component {
  render() {
    const { open, list, onClose } = this.props;

    return (
      <Dialog
        open={open}
        onClose={onClose}
      >
        ...
      </Dialog>
    )
  }
}

SelectCatDialog.propTypes = {
  open: PropTypes.bool,
  onClose: PropTypes.func,
}

SelectCatDialog.defaultProps = {
  list: [],
}

export default SelectCatDialog;

这是调用模态页面代码:
import React from 'react';
import DetailDialog from './components/DetailDialog';
import SelectProcDialog from './components/SelectProcDialog';

class Index extends React.Component {
  render() {
    return (
      ...
        <DetailDialog
          open={this.state.detailDialog}
          entity={this.state.currentDetail}
          onClose={this.handleDetailClose.bind(this)}
        />

        <SelectProcDialog
          open={this.state.catalogDialog}
          list={this.props.catalog}
          onOk={(value) => this.handleSelectCatalogOk(value)}
          onClose={() => this.setState({ catalogDialog: false })}
        />
      ...
    )
  }
}

export default Index;

发生了什么?在 v3 版本中工作正常。有人可以回答吗?

最佳答案

从 V4 开始,Dialog 和 Modal 子代必须能够持有 ref。
以下内容可以包含 ref:

  • Any Material-UI component
  • class components i.e. React.Component or React.PureComponent
  • DOM (or host) components e.g. div or button
  • React.forwardRef components
  • React.lazy components
  • React.memo components


该错误声明您将函数组件作为模态的子组件提供。
要修复错误,请将函数组件更改为可以保存引用的内容(例如类组件)。

关于reactjs - 迁移到 material-ui v4,我收到 Modal 组件的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57817994/

相关文章:

reactjs - eslint : no-case-declaration - unexpected lexical declaration in case block

javascript - 删除下拉选项使最后一个选项可见

html - 我正在尝试在新选项卡中打开此链接,这样用户就不必离开我的网站,但它不适用于 _blank

css - 使用 SX 属性创建动画 - MUI v5

reactjs - 单击菜单项后,Material UI 菜单未关闭

javascript - 如何按 ReactJS Material 表中嵌套对象中的值进行分组?

javascript - 在 React/Redux 应用程序中将 SignalR hub 放在哪里?

javascript - 三元运算符检查返回0,同时检查jsx react js

javascript - 如何识别数组的某个对象id来改变同一对象中元素的状态?

javascript - React with Material-UI 找不到模块