Bootstrap modal in React.js

前端 未结 12 1399
别跟我提以往
别跟我提以往 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条回答
  •  一个人的身影
    2020-12-02 08:09

    I've only used bootstrap cdn (css + js) to achieve "reactstrap" like solution. I've used props.children to pass dynamic data from parent to child components. You can find more about this here. In this way you have three separate components modal header, modal body and modal footer and they are totally independent from each other.


    //Modal component
    import React, { Component } from 'react';
    
    export const ModalHeader = props => {
      return 
    {props.children}
    ; }; export const ModalBody = props => { return
    {props.children}
    ; }; export const ModalFooter = props => { return
    {props.children}
    ; }; class Modal extends Component { constructor(props) { super(props); this.state = { modalShow: '', display: 'none' }; this.openModal = this.openModal.bind(this); this.closeModal = this.closeModal.bind(this); } openModal() { this.setState({ modalShow: 'show', display: 'block' }); } closeModal() { this.setState({ modalShow: '', display: 'none' }); } componentDidMount() { this.props.isOpen ? this.openModal() : this.closeModal(); } componentDidUpdate(prevProps) { if (prevProps.isOpen !== this.props.isOpen) { this.props.isOpen ? this.openModal() : this.closeModal(); } } render() { return ( ); } } export default Modal; //App component import React, { Component } from 'react'; import Modal, { ModalHeader, ModalBody, ModalFooter } from './components/Modal'; import './App.css'; class App extends Component { constructor(props) { super(props); this.state = { modal: false }; this.toggle = this.toggle.bind(this); } toggle() { this.setState({ modal: !this.state.modal }); } render() { return (

    Bootstrap Components

    This is modal header

    This is modal body

    ); } } export default App;

提交回复
热议问题