在React - Material UI中显示对话框

时间:2018-05-27 19:48:17

标签: html reactjs material-ui

我正在学习ReactJS。我想在有人点击图标时显示对话框。

以下是代码:

import React, { Component } from 'react';
import { GridList, GridTile } from 'material-ui/GridList';
import FlatButton from 'material-ui/FlatButton';
import Info from 'material-ui/svg-icons/action/info';
import { fullWhite } from 'material-ui/styles/colors';
import Dialog from 'material-ui/Dialog';
import RaisedButton from 'material-ui/RaisedButton';

(... class stuff, handleClose, handleOpen etc.)

  showDialoga() {
    const actions = [
      <FlatButton
        label="Cancel"
        primary
        onClick={this.handleClose}
      />,
      <FlatButton
        label="Submit"
        primary
        keyboardFocused
        onClick={this.handleClose}
      />,
    ];
    return (
      <div>
        <RaisedButton label="Dialog" onClick={this.handleOpen} />
        <Dialog
          title="Dialog With Actions"
          actions={actions}
          modal={false}
          open={this.state.open}
          onRequestClose={this.handleClose}
        >
          The actions in this window were passed in as an array of React objects.
        </Dialog>
      </div>
    );
  }
  render() {
    console.log(this.props);
    return (
      <div style={styles.root}>
        <GridList
          cellHeight={180}
          style={styles.gridList}
          padding={10}
        >
          {this.props.movieData.map(tile => (
            <GridTile
              key={tile.original_image}
              title={tile.title}
              actionIcon={<FlatButton
                icon={<Info color={fullWhite} />}
                style={style}
                onClick={() => this.showDialoga()}
              />}
            >
              <img src={tile.original_image} />
            </GridTile>
          ))}
        </GridList>
      </div>
    );
  }
}

我可以传递其他函数,如()=&gt; console.log(&#39;我点击了&#39;)onClick虽然我无法传递showDialoga()。

知道这是什么问题吗?

1 个答案:

答案 0 :(得分:1)

我不相信你应该如何使用对话框。

尝试将对话框打开状态设置为true / false,而不是在单击时传递React组件的返回。如果您使用函数来呈现具有事件侦听器的不同组件,也不要忘记将this绑定到类级别。