I am getting kind of desperated. I want to make an modal dialog in reactjs using bootstrap. I have installed the module with npm and imported it in my code. I tried a simple example:
import React from 'react';
import {Modal, Button } from 'react-bootstrap';
class Config extends React.Component {
constructor(){
super();
this.state = {
show: false
}
this.handleHide = this.handleHide.bind(this);
}
handleHide() {
this.setState({ show: false });
}
render(){
return ( <div>
<Button
bsStyle="primary"
bsSize="large"
onClick={() => this.setState({ show: true })}
>
Launch modal
</Button>
<Modal show={this.state.show} onHide={this.handleHide}>
<Modal.Header >
Header
</Modal.Header>
<Modal.Body>
Text
</Modal.Body>
<Modal.Footer>
<Button onClick={this.handleHide}>Close</Button>
</Modal.Footer>
</Modal>
</div>)
}
}
export default Config;
I dont get any error, but I also cant see anything, if I click on the button. I looked with the Chrome developer tool through the Elements and I can see the modal there, which is confusing.
Does someone has an idea what is wrong?
This worked for me it's almost identical to your example. However, instead of using one function to handle the state by itself, I used 2 functions for the handleClose and HandleShow.
import React from 'react';
import { Link } from 'react-router-dom';
import { Modal, Button } from 'react-bootstrap';
class Header extends React.Component {
constructor(props){
super(props);
this.handleClose = this.handleClose.bind(this);
this.handleShow = this.handleShow.bind(this);
this.state = {
show: false
}
}
handleClose(){
this.setState({
show:false
});
}
handleShow(){
this.setState({
show:true
});
}
render(){
// console.log(this.state);
return (
<div className="header-container">
<nav className="navbar navbar-dark bg-dark">
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/Contact">Contact</Link></li>
<li><Button className="login-button" onClick={this.handleShow}>LOGIN</Button></li>
</ul>
<Modal show={this.state.show} onHide={this.handleClose}>
<Modal.Header closeButton>
<Modal.Title>Modal Header</Modal.Title>
</Modal.Header>
<Modal.Body>
Modal Body
</Modal.Body>
</Modal>
</nav>
</div>
)
}
}
export default Header;
Related
i have a modal, but dont recognize their styles, what is the matter?, why dont recognize the styles? i need to import an other dependecy?
import React,{Component} from 'react'
import Modal from 'react-bootstrap/Modal'
import Button from 'react-bootstrap/Button';
class CustomModal extends Component{
constructor(props) {
super(props);
this.state = {
show: false
};
this.handleClose = this.handleClose.bind(this);
this.handleShow = this.handleShow.bind(this);
}
handleClose (){
this.setState({ show: false });
};
handleShow (){
this.setState({ show: true });
};
render() {
return(
<div>
<Button variant="primary" onClick={this.handleShow}>Update</Button>
<Modal show={this.state.show} onHide={this.handleClose}>
<Modal.Header closeButton>
<Modal.Title>Update</Modal.Title>
</Modal.Header>
<Modal.Body>...</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={this.handleClose}>Close</Button>
<Button variant="primary">Save</Button>
</Modal.Footer>
</Modal>
</div>
);
}
}
export default CustomModal;
i am using Button but my button never is painting
I simply changed the way you have imported the Components and it started working.
Changed the import to
import { Modal, Button } from "react-bootstrap";
From
import Modal from 'react-bootstrap/Modal'
import Button from 'react-bootstrap/Button';
Live demo here
https://codesandbox.io/s/react-bootstrap-x0ix0?fontsize=14&hidenavigation=1&theme=dark
Hi I have two components ExportReportRoomSelectionModal & Header both have their own logic and state. I want to open Export Report Room Selection Modal via a button on Header component and for that, I have to set State on ExportReport.... to show: true. I've tried passing handleShow function as props to Headers.js then on header I want to hook it up to the elem that triggers the ExportReport....Modal. But I don't get any props on Header at all.
import Header from './Header.js';
export default class ExportReportRoomSelectionModal extends React.Component {
constructor(props) {
super(props);
this.state = {
show: false,
};
this.handleHide = this.handleHide.bind(this);
this.handleShow = this.handleShow.bind(this);
this.handleClick = this.handleClick.bind(this);
}
handleHide() {
this.setState({show: false});
}
handleShow() {
this.setState({show: true});
}
render() {
return (
<Modal className="layout-scale-desktop layout-scale-45" onHide={this.handleHide} show={this.state.show}>
<Modal.Header closeButton>
<Modal.Title>Print PDF</Modal.Title>
</Modal.Header>
<Modal.Body>
</Modal.Body>
<Modal.Footer>
<Button bsStyle="primary" onClick={this.handleSend}>Send PDF</Button>
<Button bsStyle="primary" onClick={this.handleHide}>Close</Button>
</Modal.Footer>
<Header showModal={this.handleShow} />
</Modal>
);
}
}
class Header extends React.Component {
constructor(props) {
super(props);
this.state = { };
}
render() {
return (
<div>
<nav className="navbar navbar navbar-default">
<div className="navbar-header">
<a className="navbar-brand"
<li>
<a href="#"
data-toggle="modal"
onClick={this.props.showModal}
>
{t("reports")}
</a>
</li>
...
)
}
}
You're doing it in other way round probably? You're putting your <Header /> inside the <Modal /> which is not mounted until you click on <a/> tag. Your modal should be inside the header & props should be passed down from the header.
It should be:
Header
import ExportReportRoomSelectionModal from "./yourmodal";
class Header extends React.Component {
constructor(props) {
super(props);
this.state = {
modalIsOpen: false
};
}
toggleModal = () => {
this.setState(prevState => ({
modalIsOpen: !prevState.modalIsOpen
});
}
render() {
return (
<div>
<nav className="navbar navbar-oxehealth navbar-default">
<div className="navbar-header">
<a className="navbar-brand">
<li>
<a href="#"
data-toggle="modal"
onClick={this.showModal}
>
{t("reports")}
</a>
</li>
</a>
<ExportReportRoomSelectionModal
modalIsOpen={this.state.modalIsOpen}
toggleModal={this.toggleModal}
/>
</div>
</nav>
</div>
)
}
}
Modal
export default class ExportReportRoomSelectionModal extends React.Component {
render() {
return (
<Modal
className="layout-scale-desktop layout-scale-45"
onHide={this.props.toggleModal}
show={this.props.modalIsOpen}
>
<Modal.Header closeButton>
<Modal.Title>Print PDF</Modal.Title>
</Modal.Header>
<Modal.Body></Modal.Body>
<Modal.Footer>
<Button bsStyle="primary" onClick={this.handleSend}>
Send PDF
</Button>
<Button bsStyle="primary" onClick={this.handleHide}>
Close
</Button>
</Modal.Footer>
</Modal>
);
}
}
I am currently working on displaying a modal popup window by utilizing react modal bootstrap but it seems like something is wrong in my code. Here are my codes. I checked the following website as a reference for the codes. https://react-bootstrap.github.io/components/modal/
import React, { Component } from 'react';
import ModalList from './ModalList';
import '../css/MyPlacesEntry.css';
import '../../node_modules/bootstrap/dist/css/bootstrap.min.css';
class MyPlacesEntry extends Component {
constructor(props) {
super(props);
this.state = {
showModal: false
};
}
toggleModal() {
this.setState({
showModal: true
});
}
close() {
this.setState({
showModal: false
})
}
render() {
return (
<div>
<div className="container">
<div className="row">
<div className="col-lg-8 col-md-10 mx-auto">
{/* {console.log("111111", this)} */}
<div className="post-preview" >
<h2 className="post-menu" onClick={this.toggleModal.bind(this)}>{this.props.i.menu}</h2>
<ModalList item={this.props.i} showModal={this.state.showModal} close={this.close.bind(this)} />
<h5 className="post-comment">{this.props.i.comment}</h5>
<p className="post-meta">Posted by
{this.props.i.gmailAccount}
on january 23, 2018</p>
<hr />
</div>
</div>
</div>
</div>
</div>
)
}
}
export default MyPlacesEntry;
I was trying to send showModal as true to ModalList with onClick event but I am not sure if it is correctly coded.
import React, { Component } from 'react';
import MyPlacesEntry from './MyPlacesEntry';
import { Modal, Button } from 'react-bootstrap';
class ModalList extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<Modal {...this.props} bsSize="large" aria-labelledby="contained-modal-title-lg">
<Modal.Header closeButton>
<Modal.Title id="contained-modal-title-lg">{this.props.title}</Modal.Title>
</Modal.Header>
<Modal.Body>
<p>
price
</p>
<p>
{this.props.price}
</p>
<p>
menu
</p>
<p>
{this.props.menu}
</p>
<p>
comment
</p>
<p>
{this.props.comment}
</p>
</Modal.Body>
<Modal.Footer>
<Button onClick={this.props.onClose}>Close</Button>
</Modal.Footer>
</Modal>
);
}
}
Those codes are pretty much from the reference page but I am not exactly sure which code should be implemented to display the modal popup window. I would appreciate if you could solve this problem.
Thank you!
Use React-Bootstrap (https://react-bootstrap.github.io/components/modal). At this link, you can find examples of how to use modals or other things.
By doing this,you can use Modal as a react component like: <Modal/>
Suppose I have a modal like as follows and I would like it such that, when the modal is showing at that time I also want to work on the background.
In the following code, there is a textbox. I want to work with the textbox(or an audio) when the model is appearing.
import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import { Modal, Button } from 'react-bootstrap';
import './GenericModal.scss';
class GenericModal extends Component {
constructor(props, context) {
super(props, context);
this.state = {
showModal: false
};
this.open = this.open.bind(this);
this.close = this.close.bind(this);
}
open() {
this.setState({showModal: true});
}
close() {
this.setState({showModal: false});
}
render() {
return(
<div>
<div>I am a Bootstrap Modal</div>
<Button onClick={this.open}>Show Modal</Button>
<div>
<Modal className="modal-container" id="demo-class" ref={node => this.chart = node}
show={this.state.showModal}
onHide={this.close}
bsSize="small" backdrop={false}
>
<Modal.Header closeButton>
<Modal.Title>Modal title</Modal.Title>
</Modal.Header>
<Modal.Body>
One of fine body.........
</Modal.Body>
<Modal.Footer>
<Button onClick={this.close}>Close</Button>
<Button bsStyle="primary">Save changes</Button>
</Modal.Footer>
</Modal>
<Button onClick={this.open}>Show Modal</Button>
<input type="text" />
</div>
</div>
);
}
}
export default GenericModal;
I hope the below example flow addresses your requirement.
More form the comments in code.
i.e onClick calls wrapper function which has open method for modal together with your customizations.
import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import { Modal, Button } from 'react-bootstrap';
import './GenericModal.scss';
class GenericModal extends Component {
constructor(props, context) {
super(props, context);
this.state = {
showModal: false
};
this.open = this.open.bind(this);
this.close = this.close.bind(this);
}
open() {
let _this = this;
this.setState({showModal: true}, function(){
//state is set, i.e modal is loaded in view
//here you can do whatever like stopping audio
//_this.stopAudio();
});
}
close() {
this.setState({showModal: false});
}
playSomeAudio(){
//playAudio();
}
stopAudio(){
//stop the audio
}
doSomethingBeforeOpen(){
var _this = this;
//do whatever you want before opening model. i.e palying audio
//1. in sync:
this.playSomeAudio(); //audio starts palying before modal starts triggered
//2. in async
setTimeOut(() => {_this.playSomeAudio()}, 1); //async by setTimeout, keep your own time
//Or any
this.open(); //opens modal
}
render() {
return(
<div>
<div>I am a Bootstrap Modal</div>
<Button onClick={this.doSomethingBeforeOpen.bind(this)}>Show Modal</Button>
<div>
<Modal className="modal-container" id="demo-class" ref={node => this.chart = node}
show={this.state.showModal}
onHide={this.close}
bsSize="small" backdrop={false}
>
<Modal.Header closeButton>
<Modal.Title>Modal title</Modal.Title>
</Modal.Header>
<Modal.Body>
One of fine body.........
</Modal.Body>
<Modal.Footer>
<Button onClick={this.close}>Close</Button>
<Button bsStyle="primary">Save changes</Button>
</Modal.Footer>
</Modal>
<Button onClick={this.doSomethingBeforeOpen.bind(this)}>Show Modal</Button>
<input type="text" />
</div>
</div>
);
}
}
export default GenericModal;
I am trying React.js for the first time in trying to build this web app. I am getting Cannot read property 'setState' of null on the specified line below. I've been trying to figure out why for the past few hours and cannot seem to figure it out. Any help would be greatly appreciated.
MyModal.jsx
import React from 'react';
import { Modal, Button, ButtonToolbar } from 'react-bootstrap';
export default class MyModal extends React.Component {
constructor(props) {
super(props);
this.state = {show: false};
}
showModal() {
this.setState({show: true});
}
hideModal() {
this.setState({show: false});
}
render() {
return (
<ButtonToolbar>
<Button bsStyle="primary" onClick={this.showModal}>
Launch demo modal
</Button>
<Modal
{...this.props}
show={this.state.show}
onHide={this.hideModal}
dialogClassName="custom-modal"
>
<Modal.Header closeButton>
<Modal.Title id="contained-modal-title-lg">Modal heading</Modal.Title>
</Modal.Header>
<Modal.Body>
<h4>Wrapped Text</h4>
<p>Blah</p>
</Modal.Body>
<Modal.Footer>
<Button onClick={this.hideModal}>Close</Button>
</Modal.Footer> // Chrome inspector says it errors on this line
</Modal>
</ButtonToolbar>
);
} // end render function
} // end export default
bind your component class methods inside the constructor() (binding inside constructor is better than binding inside render() for performance sake):
constructor(props) {
super(props);
this.state = {show: false};
this.showModal = this.showModal.bind(this);
this.hideModal = this.hideModal.bind(this);
}