2015-08-21 19 views

Trả lời

5

Khi thêm các thành phần Dialog, chỉ cần thêm một ref để nó (ref="dialog" ví dụ):

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

Và sau đó bạn có thể tham khảo nó từ mã thành phần chủ sở hữu của bạn bằng cách sử dụng this.refs.dialog.onShow(...).

Trang Dialog component thực sự sử dụng refs đằng sau hậu trường, như bạn có thể thấy từ số source code.

+6

'onShow' hiện đang bị phản đối: http://material-ui.com/#/components/dialog và mở nên được xử lý thông qua 'open' prop – TrySpace

+2

Trong tất cả các ví dụ, nó hiển thị một nút có hộp thoại trên nó để khởi động nếu tôi muốn khởi động theo cách OP đang yêu cầu và ẩn nút hộp thoại. . – landed

1

Tôi khuyên bạn nên đọc Dan Abramov's answer về cách triển khai phương thức trong React.

Để sử dụng hộp thoại liệu-ui bạn có thể thay thế các DeletePostModal trong ví dụ của mình với những điều sau đây:

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) 
Các vấn đề liên quan