Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material UI modal not working (React JS)

I'm trying to replicate the example of a modal from material ui examples but I can't make it work, first I was getting a "Cannot read property 'setState' of undefined" I solved that and now there are no errors in the console but when I click on the button that displays the modal nothing happens.

Im using material-ui v1.0.0-beta.31

This is the code:

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import Typography from 'material-ui/Typography';
import Modal from 'material-ui/Modal';
import Button from 'material-ui/Button';

function rand() {
  return Math.round(Math.random() * 20) - 10;
}

function getModalStyle() {
  const top = 50 + rand();
  const left = 50 + rand();

  return {
    top: `${top}%`,
    left: `${left}%`,
    transform: `translate(-${top}%, -${left}%)`,
  };
}

const styles = theme => ({
  paper: {
    position: 'absolute',
    width: theme.spacing.unit * 50,
    backgroundColor: theme.palette.background.paper,
    boxShadow: theme.shadows[5],
    padding: theme.spacing.unit * 4,
  },
});

class SimpleModal extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      open: false
    };
    this.handleOpen = this.handleOpen.bind(this);
    this.handleClose = this.handleClose.bind(this);
  }

  handleOpen() {
    this.setState({ open: true });
  };

  handleClose(){
    this.setState({ open: false });
  };

  render() {
    const { classes } = this.props;

    return (
      <div>
        <Typography gutterBottom>Click to get the full Modal experience!</Typography>
        <Button onClick={this.handleOpen}>Open Modal</Button>
        <Modal
          aria-labelledby="simple-modal-title"
          aria-describedby="simple-modal-description"
          open={this.state.open}
          onClose={this.handleClose}
        >
          <div style={getModalStyle()} className={classes.paper}>
            <Typography type="title" id="modal-title">
              Text in a modal
            </Typography>
            <Typography type="subheading" id="simple-modal-description">
              Duis mollis, est non commodo luctus, nisi erat porttitor ligula.
            </Typography>
            <SimpleModalWrapped />
          </div>
        </Modal>
      </div>
    );
  }
}

SimpleModal.propTypes = {
  classes: PropTypes.object.isRequired,
};


const SimpleModalWrapped = withStyles(styles)(SimpleModal);

export default SimpleModalWrapped;

From the original example the only difference with the code above is that I add the following:

  constructor(props) {
    super(props);
    this.state = {
      open: false
    };
    this.handleOpen = this.handleOpen.bind(this);
    this.handleClose = this.handleClose.bind(this);
  }

Thanks!

like image 468
kennechu Avatar asked Jan 31 '18 03:01

kennechu


1 Answers

Try binding this when rendering the button:

<Button onClick={this.handleOpen.bind(this)}>Open Modal</Button>

And likewise, for modal, onClose={this.handleClose.bind(this)}, no need for these lines:

this.handleOpen = this.handleOpen.bind(this);
this.handleClose = this.handleClose.bind(this);
like image 92
sme Avatar answered Sep 29 '22 12:09

sme