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 Overflow
» npm install react-popout
javascript - Open a popUp in react
open a pop up window in reactjs application - Stack Overflow
reactjs - How can I handle an event to open a window in react js? - Stack Overflow
reactjs - React - use window.open to open a URL from a variable - Stack Overflow
Can I create a React popup with TypeScript?
What's the right way to handle popup state in a large React application?
How do I test React popups effectively?
Videos
» npm install react-new-window
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;
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.
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
» npm install reactjs-popup
There is no need to add the curly braces around your urlText variable since it's within the event callback function. If it were to be used in the JSX then curly braces would be required (like <div>{urlText}</div> or <input type="text" value={urlText} />) - but since you're calling a function there is no need to wrap the variable name in curly braces.
The error is caused because by wrapping the urlText variable in curly braces, you are converting urlText to an object: { urlText: "foo" } and thus it appears as [object%20Object] in the URL bar since any object converted to a string will be literally '[object Object]'.
onClick={(event) => {
event.preventDefault();
window.open(urlText, "_blank");
}}
Simply pass the string urlText directly into the function: window.open(urlText, '_blank') without curly brackets.
The curly brackets is syntax to create an object, this function takes in a string argument as its first parameter. So javascript parses your argument into a string [object%20Object], and due to there being no protocol in that it recognises it as a local path.
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.