Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

javascript - React Material UI modal not opening

I'm trying to put together a react component from material UI that opens a modal. I have the state for the toggle and I see the toggle change from false to true in the react developer console in chrome but the modal does not open when I click the button (the button only changes the state verified by react console).

import react from 'react'
import Modal from '@material-ui/core/Modal';
import Button from '@material-ui/core/Button';
import { withStyles } from "@material-ui/core/styles";

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: 400,
      backgroundColor: theme.palette.background.paper,
      border: '2px solid #000',
      boxShadow: theme.shadows[5],
      padding: theme.spacing(2, 4, 3),
    },
  }));


class ModalTest extends react.Component {
    

    state = {
        modalToggle: false
    }

    componentDidMount(){
        
    }

    handleOpen = () => this.setState({modalToggle: true})

    handleClose = () => this.setState({modalToggle: false})
    
    renderModalBody(){
        return (
            <div style={getModalStyle()} className={styles.paper}>
                <h2 id="simple-modal-title">Text in a modal</h2>
                <p id="simple-modal-description">
                This is a test of modal.
                </p>
          </div>
        );
    }

    renderActionButtons(){
        return <Button onClick={this.handleOpen} style={{margin: 0, position: 'absolute',
                                                         top: '50%', left: '50%' }} variant="contained" color="primary">Open Modal</Button>
    }

    renderModal(){
        <Modal open={this.state.modalToggle} onClose={this.handleClose}>{this.renderModalBody()}</Modal>
    }

    render(){
        return this.renderActionButtons()
    }

}

export default withStyles(styles, { withTheme: true })(ModalTest)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

as @Christian Fritz said you didn't call the method to render the model

now you can do the render in the render return like this

render(){
return (
   <Button onClick={this.handleOpen} style={{margin: 0, position: 'absolute',
                                                top: '50%', left: '50%' }} 
   variant="contained" color="primary">Open Modal</Button>
    <Modal open={this.state.modalToggle} onClose={this.handleClose}>{this.renderModalBody()}</Modal>
    )

}

because material ui will not open the model until it value is true and I recommend to you to use functional component because is much more easy and can be handle better because every state is manage by itself and you can do almost the same stuff as class component plus as it seems react team try to push the use of functional component more I write a sample code is function component :

export default UserShow;
const ModelTest = () => {
  const [open, setOpen] = useState(false);

  return (
    <>
      <Button
        onClick={() => setOpen(true)}
        style={{ margin: 0, position: "absolute", top: "50%", left: "50%" }}
        variant="contained"
        color="primary"
      >
        Open Modal
      </Button>

      <Modal open={open} onClose={() => setOpen(false)}>
        <div style={getModalStyle()} className={styles.paper}>
          <h2 id="simple-modal-title">Text in a modal</h2>
          <p id="simple-modal-description">This is a test of modal.</p>
        </div>
      </Modal>
    </>
  );
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...