KarlR
KarlR

Reputation: 1615

Show dialog in React - Material UI

I am learning ReactJS. I would like to display dialog when someone clicks on the icon.

Here is the code:

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>
    );
  }
}

I am able to pass other function like () => console.log('I am clicked') to onClick although I am not able to pass that showDialoga().

Any idea what is the problem?

Upvotes: 0

Views: 1889

Answers (1)

forJ
forJ

Reputation: 4617

I do not believe that's how you are supposed to use dialog.

Instead of passing return of React component on click, try setting the dialog opened state to be true/false. Also do not forget to bind this to the class level if you are using functions to render different components that has event listeners.

Upvotes: 1

Related Questions