Bootstrap modal in React.js

前端 未结 12 1371
别跟我提以往
别跟我提以往 2020-12-02 07:19

I need to open a Bootstrap Modal from clicking on a button in a Bootstrap navbar and other places (to show data for a component instance, ie. providing \"editing\" f

12条回答
  •  Happy的楠姐
    2020-12-02 08:18

    The quickest fix would be to explicitly use the jQuery $ from the global context (which has been extended with your $.modal() because you referenced that in your script tag when you did ):

      window.$('#scheduleentry-modal').modal('show') // to show 
      window.$('#scheduleentry-modal').modal('hide') // to hide
    

    so this is how you can about it on react

    import React, { Component } from 'react';
    
    export default Modal extends Component {
        componentDidMount() {
            window.$('#Modal').modal('show');
        }
    
        handleClose() {
            window.$('#Modal').modal('hide');
        }
    
        render() {
            <
            div className = 'modal fade'
            id = 'ModalCenter'
            tabIndex = '-1'
            role = 'dialog'
            aria - labelledby = 'ModalCenterTitle'
            data - backdrop = 'static'
            aria - hidden = 'true' >
                <
                div className = 'modal-dialog modal-dialog-centered'
            role = 'document' >
                <
                div className = 'modal-content' >
                // ...your modal body
                <
                button
            type = 'button'
            className = 'btn btn-secondary'
            onClick = {
                    this.handleClose
                } >
                Close <
                /button> < /
            div > <
                /div> < /
            div >
        }
    }
    
    

提交回复
热议问题