Here is modern way to achieve this using React Hooks
import React, { useState } from "react";
const PopUp = ({ idMessage }) => {
// create state `open` with default as false
const [open, setOpen] = useState(false);
return (
<>
{/* click of button toggles `open` value therefore visibility */}
<button
onClick={() => setOpen(!open)}
type="button"
className="btn btn-primary"
data-toggle="modal"
data-target={`#${idMessage}`}
>
{idMessage}
</button>
{/* If open is true show your <div /> */}
{open && (
<div
className="modal fade"
id={idMessage}
tabIndex="-1"
role="dialog"
aria-labelledby="exampleModalLabel"
aria-hidden="true"
>
content
</div>
)}
</>
);
};
export default PopUp;
Answer from vitjbr on Stack OverflowHere is modern way to achieve this using React Hooks
import React, { useState } from "react";
const PopUp = ({ idMessage }) => {
// create state `open` with default as false
const [open, setOpen] = useState(false);
return (
<>
{/* click of button toggles `open` value therefore visibility */}
<button
onClick={() => setOpen(!open)}
type="button"
className="btn btn-primary"
data-toggle="modal"
data-target={`#${idMessage}`}
>
{idMessage}
</button>
{/* If open is true show your <div /> */}
{open && (
<div
className="modal fade"
id={idMessage}
tabIndex="-1"
role="dialog"
aria-labelledby="exampleModalLabel"
aria-hidden="true"
>
content
</div>
)}
</>
);
};
export default PopUp;
You could consider creating the popup as a re-usable component, that just renders the props.message.
Suppose you have the button in App.js, here is how you can add the click listener on it.
class App extends Component {
state = {showPopup: false};
openPopupHandler = () => {
this.setState({showPopup: true});
}
closePopupHandler = () => {
this.setState({showPopup: false});
}
render() {
let popup = null;
if(this.state.showPopup) {
popup = (<Popup message='the text you need to display' closeMe={this.closePopupHandler}/>);
}
return(
<div>
<button onClick={this.clicked}>Click me </button>
{popup}
</div>
);
}
}
And you can define the popup component as given below.
Popup.js
const popup = (props) => {
return (
<div>
<p>{props.message}</p>
<button onClick={props.closeMe}>Close Popup</button>
</div>
);
}
Also, style the popup component with the size as per your requirement and with an z-index greater than that of the parent component.
isOpen is not a property of this, but of state.
You have to write:
{this.state.isOpen && <Popup...
Here is the documentation in case you want to read about it yourself
You should call state values proper way. See examples here. And also you should fix the togglePopup function:
togglePopup = () => {
this.setState(
{ isOpen: !this.state.isOpen}
)
}
So it'll be changed to false when you call it in the handleClose function.
» npm install reactjs-popup
Can I create a React popup with TypeScript?
How do React popups impact SEO?
How do I test React popups effectively?
They called Modal.
You can check the react-modal library.
Here is a simple example of react-modal being used in an app with some custom styles and focusable input elements within the modal content:
import React from 'react';
import ReactDOM from 'react-dom';
import Modal from 'react-modal';
const customStyles = {
content : {
top : '50%',
left : '50%',
right : 'auto',
bottom : 'auto',
marginRight : '-50%',
transform : 'translate(-50%, -50%)'
}
};
// Make sure to bind modal to your appElement (http://reactcommunity.org/react-modal/accessibility/)
Modal.setAppElement('#yourAppElement')
class App extends React.Component {
constructor() {
super();
this.state = {
modalIsOpen: false
};
this.openModal = this.openModal.bind(this);
this.afterOpenModal = this.afterOpenModal.bind(this);
this.closeModal = this.closeModal.bind(this);
}
openModal() {
this.setState({modalIsOpen: true});
}
afterOpenModal() {
// references are now sync'd and can be accessed.
this.subtitle.style.color = '#f00';
}
closeModal() {
this.setState({modalIsOpen: false});
}
render() {
return (
<div>
<button onClick={this.openModal}>Open Modal</button>
<Modal
isOpen={this.state.modalIsOpen}
onAfterOpen={this.afterOpenModal}
onRequestClose={this.closeModal}
style={customStyles}
contentLabel="Example Modal"
>
<h2 ref={subtitle => this.subtitle = subtitle}>Hello</h2>
<button onClick={this.closeModal}>close</button>
<div>I am a modal</div>
<form>
<input />
<button>tab navigation</button>
<button>stays</button>
<button>inside</button>
<button>the modal</button>
</form>
</Modal>
</div>
);
}
}
ReactDOM.render(<App />, appElement);
You can see more examples here
The popup is called a modal. There are multiple libraries that support modals. React bootstrap provides a neat modal. Or if you want to use modal alone there are libraries such as react-modal and react-responsive-modal
For class component
In app.js
import React from "react";
import Modal from "./Component/Modal";
import "./styles.css";
class App extends React.Component {
state = {
show: false
};
showModal = e => {
this.setState({
show: !this.state.show
});
};
render() {
return (
<div className="App">
<button
class="toggle-button"
id="centered-toggle-button"
onClick={e => {
this.showModal(e);
}}
>
{" "}
show Modal{" "}
</button>
<Modal onClose={this.showModal} show={this.state.show}>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nobis
deserunt corrupti, ut fugit magni qui quasi nisi amet repellendus non
</Modal>
</div>
);
}
}
export default App;
In component/modal
Make file named index.js
import React from "react";
import "./modal.css";
import PropTypes from "prop-types";
export default class Modal extends React.Component {
onClose = e => {
this.props.onClose && this.props.onClose(e);
};
render() {
if (!this.props.show) {
return null;
}
return (
<div class="modal" id="modal">
<h2>Modal Window</h2>
<div class="content">{this.props.children}</div>
<div class="actions">
<button class="toggle-button" onClick={this.onClose}>
close
</button>
</div>
</div>
);
}
}
Modal.propTypes = {
onClose: PropTypes.func.isRequired,
show: PropTypes.bool.isRequired
};
Make file modal.css
html,
body {
height: 100%;
}
body {
background: #eee;
display: flex;
justify-content: center;
align-items: center;
}
.modal {
width: 500px;
background: white;
border: 1px solid #ccc;
transition: 1.1s ease-out;
box-shadow: -2rem 2rem 2rem rgba(0, 0, 0, 0.2);
filter: blur(0);
transform: scale(1);
opacity: 1;
visibility: visible;
}
.modal.off {
opacity: 0;
visibility: hidden;
filter: blur(8px);
transform: scale(0.33);
box-shadow: 1rem 0 0 rgba(0, 0, 0, 0.2);
}
@supports (offset-rotation: 0deg) {
offset-rotation: 0deg;
offset-path: path("M 250,100 S -300,500 -700,-200");
.modal.off {
offset-distance: 100%;
}
}
@media (prefers-reduced-motion) {
.modal {
offset-path: none;
}
}
.modal h2 {
border-bottom: 1px solid #ccc;
padding: 1rem;
margin: 0;
}
.modal .content {
padding: 1rem;
}
.modal .actions {
border-top: 1px solid #ccc;
background: #eee;
padding: 0.5rem 1rem;
}
.modal .actions button {
border: 0;
background: #78f89f;
border-radius: 5px;
padding: 0.5rem 1rem;
font-size: 0.8rem;
line-height: 1;
}
#centered-toggle-button {
position: absolute;
}
This is an example of modal with class component. Please check if this helps you.
@Python
Second example
You can try this, if that does not work. This is bit easy also.
Using react bootstrap module. In App.js
import React from 'react';
import './App.css';
import { Button,Modal} from 'react-bootstrap';
class App extends React.Component {
constructor(){
super();
this.state={
show:false
}
}
handleModal(){
this.setState({show:!this.state.show})
}
render(){
return (
<div>
<h2 align='center'>Example of Modal in Reactjs</h2>
<div className="modalClass">
<Button onClick={()=>this.handleModal()}>Click To Open Modal</Button>
</div>
<Modal show={this.state.show} onHide={()=>this.handleModal()}>
<Modal.Header closeButton>This is a Modal Heading</Modal.Header>
<Modal.Body>This is a Modal Body</Modal.Body>
<Modal.Footer>
<Button onClick={()=>this.handleModal()}>Close</Button>
<Button onClick={()=>this.handleModal()}>Save</Button>
</Modal.Footer>
</Modal>
</div>
)
}
}
export default App;
In css file just add
.modalClass {
text-align: center;
margin-top: 100px;
}
The first step is to import packages.
import React from "react";
import { Modal, Button, Form } from "react-bootstrap";
import "bootstrap/dist/css/bootstrap.css";
Then in App function you can set state to show and hide the modal popup.
function App() {
const [show, setShow] = useState(false);
const handleShow = () => setShow(true);
return (
<>
<div
className="d-flex align-items-center justify-content-center"
style={{ height: "100vh" }}
>
<Button variant="primary" onClick={handleShow}>
Launch Form modal
</Button>
</div>
<Modal show={show}>
<Modal.Header closeButton>
<Modal.Title>Login Form</Modal.Title>
</Modal.Header>
<Modal.Body>
<></>
</Modal.Body>
<Modal.Footer>
<Button variant="secondary">Close Modal</Button>
</Modal.Footer>
</Modal>
</>
);
}
This is just an example, hope this helps you.
» npm install react-new-window