简体   繁体   English

为什么单击关闭按钮时我的模态不会关闭?

[英]Why won't my modal close when clicking the close button?

I've hit a wall and have no clue what to do.我碰壁了,不知道该怎么办。 I'm simply trying to close my bootstrap modal but it just won't close.我只是想关闭我的引导模式,但它不会关闭。 It opens fine but again it just won't close.它可以正常打开,但又不会关闭。 I've tried so many ways to rectify this that there won't be enough space on this post to list all my attempts.我已经尝试了很多方法来纠正这个问题,所以这篇文章没有足够的空间来列出我所有的尝试。

What am I doing wrong and how can I fix this?我做错了什么,我该如何解决?

Here's App.js:这是 App.js:

import React, { Component } from 'react';
import Product from './Product';
import { ProductConsumer } from "../context";
import TitleBody from "./TitleBody";
import AboutButton from "./AboutButton";
import AboutButtonModal from "./AboutButtonModal";

export default class App extends Component {
    constructor(props) {
        console.log("Props - ", props);
        super(props);
        this.state = {
            modalVisible: false
        };
        this.openModal = this.openModal.bind(this);
    }

    openModal() {
        console.log("Open modal called ", this.state.modalVisible);
        const modalVisible = !this.state.modalVisible;
        this.setState({
            modalVisible
        }, console.log(this.state.modalVisible));
    }

    render() {
        let styles = this.state.modalVisible
            ? { display: "block" }
            : { display: "none" };
        return (
            <React.Fragment>
                <div className="py-5">
                    <div className="container">
                        <TitleBody name="Welcome to" title="Cruskip"/>
                        <AboutButtonModal show={this.state.modalVisible} onClick={this.openModal} style={styles}/>
                        <AboutButton onClick={this.openModal}/>
                        </div>
                    </div>
                </div>
            </React.Fragment>
        );
    }
}

Here's AboutButtonModal.js:这是关于ButtonModal.js:

import React from 'react';
import './AboutButtonModal.scss';

const Modal = ({ handleClose, show, children}, props) => {
    const showHideClassname = show ? "modal display-block" : "modal display-none";

    return(
        <div className={showHideClassname} style={props.style}>
            <div className="modal-dialog">
                <div className="modal-content">
                    <div className="modal-header">
                        <button
                            type="button"
                            onClick={props.onClick}
                            className="close"
                        >
                            &times;
                        </button>
                        <h4 className="modal-title">Modal Header</h4>
                    </div>
                    <div className="modal-body">
                        <p>Some text in the modal.</p>
                    </div>
                    <div className="modal-footer">
                        <button
                            onClick={props.onClick}
                            type="button"
                            className="btn btn-default"
                        >
                            Close
                        </button>
                    </div>
                </div>
            </div>
        </div>
    );
};

export default Modal;

Here's AboutButton.js:这是关于Button.js:

import React from 'react';
import './AboutButton.scss';

const AboutButton = (props) => {
    return(
        <div className="row">
            <button className="about-btn" type="button" onClick={props.onClick}>
                About Us
            </button>
        </div>
    );
};

export default AboutButton;

If you are using ({ handleClose, show, children }, props),It won't give you to access of props onClick function..如果您正在使用 ({ handleClose, show, children }, props),它不会让您访问 props onClick function..

So to use props, just apply object destructing of props..Now,Your code will become like this...所以要使用道具,只需应用 object 破坏道具..现在,您的代码将变成这样...

const Modal = ({ handleClose, show, children,...props})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM