在 React 上显示对话框 material-ui

Show Dialog on React material-ui

我正在为我的 React 应用程序使用 material-ui 的 Dialog component。如何将组件设置为变量以便调用 onShow() 方法?

添加Dialog组件时,只需为其添加一个ref(例如ref="dialog"):

<Dialog ref="dialog" title="..." actions="...">
  dialog content
</Dialog>

然后您可以使用 this.refs.dialog.onShow(...).

从您的所有者组件代码中引用它

Dialog component page actually uses refs behind the scenes, as you can see from its source code.

我建议阅读 如何在 React 中实现模态。

为了使用 material-ui 对话框,您可以将示例中的 DeletePostModal 替换为以下内容:

import { deletePost, hideModal } from '../actions';
import React, {Component} from 'react';
import {connect} from "react-redux";
import {Button, Dialog, DialogActions, DialogTitle} from "material-ui";
import PropTypes from 'prop-types';

const DeletePostModal = ({ post, dispatch }) => (
  <Dialog open={true}>
       <DialogTitle>Delete post {post.name}?</DialogTitle>
            <DialogActions>
                 <button onClick={() => {
                      dispatch(deletePost(post.id)).then(() => {
                           dispatch(hideModal());
                      });
                  }}>
                        Yes
                  </button>
                  <button onClick={() => dispatch(hideModal())}>
                        Nope
                  </button>
        </DialogActions>
   </Dialog>
)

export default connect(
  (state, ownProps) => ({
    post: state.postsById[ownProps.postId]
  })
)(DeletePostModal)