【问题标题】:Show Dialog on React material-ui在 React Material-ui 上显示对话框
【发布时间】:2015-11-15 21:01:49
【问题描述】:

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

【问题讨论】:

    标签: javascript reactjs material-ui


    【解决方案1】:

    我建议阅读Dan Abramov's answer,了解如何在 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)
    

    【讨论】:

      【解决方案2】:

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

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

      然后您可以使用 this.refs.dialog.onShow(...) 从您的所有者组件代码中引用它。

      Dialog component 页面实际上在幕后使用了 refs,正如您从其 source code 中看到的那样。

      【讨论】:

      • onShow 现在已弃用:material-ui.com/#/components/dialog 并且应通过 open 属性处理打开
      • 在所有示例中,它都显示了一个带有对话框的按钮,用于启动如果我想以 OP 要求的方式启动并隐藏对话框按钮..
      猜你喜欢
      • 2020-11-10
      • 2019-02-28
      • 2017-03-27
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      相关资源
      最近更新 更多