I have a component which displays a data. I have to open this component in a new window on clicking a button/ link from a parent component.
export default class Parent extends Component {
construtor(props) {
super(props);
}
viewData = () => {
window.open('childcomponent.js','Data','height=250,width=250');
}
render() {
return (
<div> <a onclick={this.viewData}>View Data</a></div>
)
}
}
I dont know how to invoke another component and also display it in a new size specified window.
Actually I need to send a props to that child component with which it will fetch me the data from database and render it.
You can use
ReactDOM.createPortal
to render a component in a new window as David Gilbertson explains in his post:This answer is based on David Gilbertson's post. It has been modified to work in Edge. To make this work in Edge
div
andstyle
elements must be created with the window into which they will be rendered.The full modified source can be found here https://codepen.io/iamrewt/pen/WYbPWN
You wouldn't open the component directly. You'll need a new page/view that will show the component. When you open the window, you'll then point it at the appropriate URL.
As for size, you provide it as a string in the third parameter of open, which you actually have correct:
Note, however, that browsers may, for a variety of reasons, not respect your width and height request. For that reason, it's probably a good idea to also apply appropriate CSS to get a space the right size in case it does open larger than you wanted.