简体   繁体   English

在 React Js 中,我想尝试在点击卡片后打开模态,但在点击卡片后没有打开模态

[英]In React Js I want to try to open modal after clicking on card but the modal is not open after clicking on card

Please help me to sort out this problem I want to open modal after clicking n card.请帮我解决这个问题我想在点击n卡后打开模态。 I have use button and under that, I use card.我有使用按钮,在此之下,我使用卡。

    import React from 'react';
    import ReactDOM from 'react-dom';
    import 'bootstrap/dist/css/bootstrap.min.css';
    import { Button } from 'react-bootstrap';
    import CardDesing from './CardDesing'
    import Modals from './Modal';


    class App extends React.Component {

        render() {
            return (

                <Button onClick={this.onCardClick} className="unstyled-button">
                    <CardDesing /> 
                </Button>

            );
        }

        onCardClick = () => {
            console.log("hello");
            return (
                <Modals />
            );
        }
    }

    ReactDOM.render(<App />, document.querySelector("#root"))

You can render your model conditionally.您可以有条件地渲染模型。 if button is clicked then we are showing model如果单击按钮,则我们正在显示模型

import React from 'react';
import ReactDOM from 'react-dom';
import 'bootstrap/dist/css/bootstrap.min.css';
import { Button } from 'react-bootstrap';
import CardDesing from './CardDesing'
import Modals from './Modal';

class App extends React.Component {

    state = {
      isClicked : false // button clicked or not 
    }

   // set new state to your isClicked
   onCardClick = () => {
       this.setState({ isClicked : !this.state.isClicked }) 
    }

    render() {
        return (
          <>
            <Button onClick={this.onCardClick} className="unstyled-button">
                <CardDesing /> 
            </Button>
            {
                this.state.isClicked ?   <Modals /> : '' 
            }
         </>
        );
    }
}

ReactDOM.render(<App />, document.querySelector("#root"))

Render modal and just show it on click:渲染模态并在单击时显示它:

state = {
  show: false
}
render() {
  return (
    <>
     <Button onClick={this.onCardClick} className="unstyled-button">
        <CardDesing /> 
     </Button>
     { this.state.show && <Modals /> || '' }
   </>
  );
}

onCardClick = () => {
  this.setState({ show: true })
}

According to react-bootstrap docs根据react-bootstrap 文档

You should use the property: show : When true The modal will show itself.您应该使用属性: show : When true 模态将显示自己。

Example:例子:

function Example() {
    const [show, setShow] = useState(false);

    const handleClose = () => setShow(false);
    const handleShow = () => setShow(true);

    return (
        <div>
            <Button variant="primary" onClick={handleShow}>
                Launch demo modal
            </Button>

            <Modal show={show} onHide={handleClose}>
                {/*  */}
            </Modal>
        </div>
    );
}

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

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