【问题标题】:react-portal onOpen Not Firingreact-portal onOpen 未触发
【发布时间】:2020-03-31 05:28:13
【问题描述】:

由于最近将 React 升级到 16.8.6,正尝试从 react-portal 从 v2 迁移到 v4。

卡在 Portal,即使isOpen=true 时对话框也不会显示。发现onOpen 没有触发。关于如何更改代码的任何建议?

import * as React from 'react';
import { Portal } from 'react-portal';

import 'dialog-polyfill/dialog-polyfill.css';
import 'dialog-polyfill/dialog-polyfill.js';

import { Dialog, DialogTitle, DialogContent, DialogActions, Button } from 'react-mdl';

class Confirm extends React.Component {
  onOpen() {
    if (!this.dialog.showModal) {
      dialogPolyfill.registerDialog(this.dialog);
    }

    this.dialog.showModal();
  }

  closeDialog() {
    this.dialog.close();
    this.portal.closePortal();
  }

  render() {
    const props = this.props;

    return (
      <Portal ref={c => this.portal = c} onOpen={this.onOpen.bind(this)} isOpen={Boolean(props.callback)} {...props}>
        <dialog ref={c => this.dialog = c} className="mdl-dialog" style={props.style}>
          <DialogTitle>{props.title}</DialogTitle>
          <DialogContent>
            {props.message}
          </DialogContent>
          <DialogActions>
            <Button type='button' onClick={() => {this.closeDialog(); props.confirm();}}>Confirm</Button>
            <Button type='button' onClick={() => {this.closeDialog(); props.dismissConfirmation();}}>Cancel</Button>
          </DialogActions>
        </dialog>
      </Portal>
    );
  }
}

export default Confirm;

预期结果:会弹出一个确认对话框。

在浏览器中,已经可以通过修改css看到对话框了。

【问题讨论】:

    标签: react-portal


    【解决方案1】:

    最终意识到这是由于 openByClickOn 不再受支持。如中所述:

    openByClickOn Not Supported In React-Portal v4

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多