9

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

4

2 回答 2

8

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

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

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

Dialog 组件页面实际上在幕后使用了 refs,从它的源代码可以看出。

于 2015-08-22T23:30:00.997 回答
2

我建议阅读Dan Abramov关于如何在 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)
于 2017-11-15T06:41:07.917 回答