You can use ReactDOM.createPortal to render a component in a new window as David Gilbertson explains in his post:

class MyWindowPortal extends React.PureComponent {
  constructor(props) {
    super(props);
    // STEP 1: create a container <div>
    this.containerEl = document.createElement('div');
    this.externalWindow = null;
  }
  
  render() {
    // STEP 2: append props.children to the container <div> that isn't mounted anywhere yet
    return ReactDOM.createPortal(this.props.children, this.containerEl);
  }

  componentDidMount() {
    // STEP 3: open a new browser window and store a reference to it
    this.externalWindow = window.open('', '', 'width=600,height=400,left=200,top=200');

    // STEP 4: append the container <div> (that has props.children appended to it) to the body of the new window
    this.externalWindow.document.body.appendChild(this.containerEl);
  }

  componentWillUnmount() {
    // STEP 5: This will fire when this.state.showWindowPortal in the parent component becomes false
    // So we tidy up by closing the window
    this.externalWindow.close();
  }
}
Answer from CD.. on Stack Overflow
Top answer
1 of 7
35

You can use ReactDOM.createPortal to render a component in a new window as David Gilbertson explains in his post:

class MyWindowPortal extends React.PureComponent {
  constructor(props) {
    super(props);
    // STEP 1: create a container <div>
    this.containerEl = document.createElement('div');
    this.externalWindow = null;
  }
  
  render() {
    // STEP 2: append props.children to the container <div> that isn't mounted anywhere yet
    return ReactDOM.createPortal(this.props.children, this.containerEl);
  }

  componentDidMount() {
    // STEP 3: open a new browser window and store a reference to it
    this.externalWindow = window.open('', '', 'width=600,height=400,left=200,top=200');

    // STEP 4: append the container <div> (that has props.children appended to it) to the body of the new window
    this.externalWindow.document.body.appendChild(this.containerEl);
  }

  componentWillUnmount() {
    // STEP 5: This will fire when this.state.showWindowPortal in the parent component becomes false
    // So we tidy up by closing the window
    this.externalWindow.close();
  }
}
2 of 7
33

The upvoted answer works great!

Just leaving a function component version here in case people are searching for that in the future.

const RenderInWindow = (props) => {
  const [container, setContainer] = useState(null);
  const newWindow = useRef(null);

  useEffect(() => {
    // Create container element on client-side
    setContainer(document.createElement("div"));
  }, []);

  useEffect(() => {
    // When container is ready
    if (container) {
      // Create window
      newWindow.current = window.open(
        "",
        "",
        "width=600,height=400,left=200,top=200"
      );
      // Append container
      newWindow.current.document.body.appendChild(container);

      // Save reference to window for cleanup
      const curWindow = newWindow.current;

      // Return cleanup function
      return () => curWindow.close();
    }
  }, [container]);

  return container && createPortal(props.children, container);
};
Discussions

React - How to open a component in a new window on a button click?
I am creating a project in React and I am trying to open a canvasjs graph in another window on a button click. Below is the code to the loadData.js file that when the program is ran I will click on... More on stackoverflow.com
🌐 stackoverflow.com
Is it possible to render a component in a new tab in reactjs without setting its route in any page?
I'm not sure if I follow. Are you wanting to render "just" the component in a new tab and not the entire application around the component? Opening a new tab is easy, sure. You just create a link with target set to "_blank" though generally it's not recommended so that you leave the option to the end user how to interact with a link. Or if you're doing it programmatically, you'd use the `window.open` function. From there, it's just a matter of making a route for that component in your app. The gist is that you'd want to create a separate route in your routing system that lacks your app's layout and directly renders the component. You'd likely want/need to pass any props as route or search params. That said, if for some reason you really wanted to avoid this (not sure why) and you had a static component after rendering, you could theoretically use a combination of `window.open` to open a new tab and save a reference to it, ReactDOM's renderToString to generate the raw HTML from a component with any props you want to pass in, and `document.write` on the opened window reference to set the content of the tab to that generated HTML. This is incredibly hacky and I don't even know if it will work properly. You'll likely have mixed support from browsers now and going forward, and again this wouldn't allow the component to update dynamically. More on reddit.com
🌐 r/react
5
1
February 22, 2024
Render a react component in a new window
I am struggling to implement such that upon clicking the print button, a new tab will open containing the ready to print HTML certificate so that the user will only CTRL + P to have the certificate printed. How do i render my react certificate component in a new window? More on stackoverflow.com
🌐 stackoverflow.com
Is there any way to open a new tab in react without refreshing the page.
What page should not refresh? The newly opened one or the one you already are? More on reddit.com
🌐 r/webdev
20
0
June 5, 2024
🌐
npm
npmjs.com › package › react-new-window
react-new-window - npm
November 27, 2022 - Support the full window.open api. Built for React 16 (uses ReactDOM.createPortal). Handler for blocked popups (via onBlock prop). Center popups according to the parent window or screen. ... import React from 'react' import NewWindow from 'react-new-window' const Demo = () => ( <NewWindow> <h1>Hi 👋</h1> </NewWindow> )
      » npm install react-new-window
    
Published   Nov 27, 2022
Version   1.0.1
Author   Rubens Mariuzzo
🌐
GitHub
github.com › rmariuzzo › react-new-window
GitHub - rmariuzzo/react-new-window: 🔲 Pop new windows in React, using `window.open`.
🔲 Pop new windows in React, using `window.open`. Contribute to rmariuzzo/react-new-window development by creating an account on GitHub.
Starred by 464 users
Forked by 108 users
Languages   JavaScript 98.4% | HTML 1.6%
🌐
Scott Logic
blog.scottlogic.com › 2019 › 10 › 29 › popout-windows-in-react.html
Popout Windows in React
October 29, 2019 - import React from 'react'; import ReactDOM from 'react-dom'; interface Props { title: string; // The title of the popout window closeWindow: () => void; // Callback to close the popout } interface State { externalWindow: Window | null; // The ...
🌐
CodeSandbox
codesandbox.io › s › react-new-window-klsex
React new window - CodeSandbox
July 12, 2019 - React new window by Kater-auf-Dach using react, react-dom, react-scripts
Published   Jul 12, 2019
Author   Kater-auf-Dach
🌐
Medium
enetoolveda.medium.com › open-a-popup-window-react-db81c8c180e5
Open a PopUp Window, React. Today I will try to show you how to… | by Ernesto Jara Olveda | Medium
February 10, 2020 - I called it touch ./src/window... to open it no matter what. There is only one rule we must follow. The new window must be open by the users actions....
Find elsewhere
🌐
GitHub
github.com › RyanNerd › react-new-improved-window
GitHub - RyanNerd/react-new-improved-window: React New and Improved Window popup using the window.open API
import React from 'react' import NewImprovedWindow from 'react-new-improved-window' const Demo = () => ( <Index> <h1>Hi 👋</h1> </Index> ) When <Index /> is mounted a popup window will be opened.
Starred by 15 users
Forked by 2 users
Languages   TypeScript 50.3% | JavaScript 41.3% | HTML 6.9% | CSS 1.5% | TypeScript 50.3% | JavaScript 41.3% | HTML 6.9% | CSS 1.5%
🌐
Stack Overflow
stackoverflow.com › questions › 64217570 › react-how-to-open-a-component-in-a-new-window-on-a-button-click
React - How to open a component in a new window on a button click?
Is there something in React that I can use to do this? the window.open() function does not seem to be working. Thank you! openNewWindow = () => { const newWindow = window.open(); newWindow.document.write(<MultiSeriesChart dataFromParent = {this.state} parentCallback/>); } render() { return ( <div> <title>Program Controller</title> <h1>Program Controls: </h1> <button onClick={this.pressedLoad}>Load Data</button> <button id="multiSeriesChart" onClick={this.openNewWindow}>Open Multi-Series Graph</button> </div> ); }; };
🌐
npm
npmjs.com › package › react-window-open
react-window-open - npm
January 18, 2023 - import React, { useState } from 'react' import { NewWindow } from 'react-window-open' const Example = () => { const [isOpen, setIsOpen] = useState(false) const [counter, setCounter] = useState(0) return ( <> <button onClick={() => ...
      » npm install react-window-open
    
Published   Jan 18, 2023
Version   1.1.0
Author   Axel Milliery
🌐
npm
npmjs.com › search
window.open - npm search
This is a window launcher for front-end compatibility with browser issues when using window.open. ... imagine10255• 2.0.6 • 8 months ago • 0 dependents • MITpublished version 2.0.6, 8 months ago0 dependents licensed under $MIT ... A small reactjs utility to make sure that window.open actually opens popup and cominecate with the parent window.opner
🌐
Reddit
reddit.com › r/react › is it possible to render a component in a new tab in reactjs without setting its route in any page?
r/react on Reddit: Is it possible to render a component in a new tab in reactjs without setting its route in any page?
February 22, 2024 - That said, if for some reason you really wanted to avoid this (not sure why) and you had a static component after rendering, you could theoretically use a combination of `window.open` to open a new tab and save a reference to it, ReactDOM's renderToString to generate the raw HTML from a component with any props you want to pass in, and `document.write` on the opened window reference to set the content of the tab to that generated HTML.
🌐
CodeSandbox
codesandbox.io › examples › package › react-new-window
react-new-window examples - CodeSandbox
AboutPop a new window in React, using window.open API69,847Weekly Downloads · Latest version1.0.1 · LicenseMIT · External Links · github.com/rmariuzzo/react-new-window#readme · github.com/rmariuzzo/react-new-window/issues ·
Top answer
1 of 4
2

New Solution based on CreatePortal

import React, { useEffect, useCallback, useMemo, useState } from "react";
import { render, createPortal } from "react-dom";

const App = () => {
  const [isOpen, setOpenState] = useState(false);
  const open = useCallback(() => setOpenState(true));
  const close = useCallback(() => setOpenState(false));

  return (
    <div>
      <h1>Portals in React</h1>
      <button onClick={open}>Open</button>
      <button onClick={close}>Close</button>
      {isOpen && (
        <NewWindow close={close}>
          Example <button onClick={close}>Close</button>
        </NewWindow>
      )}
    </div>
  );
};

const NewWindow = ({ children, close }) => {
  const newWindow = useMemo(() =>
    window.open(
      "about:blank",
      "newWin",
      `width=400,height=300,left=${window.screen.availWidth / 2 -
        200},top=${window.screen.availHeight / 2 - 150}`
    )
  );
  newWindow.onbeforeunload = () => {
    close();
  };
  useEffect(() => () => newWindow.close());
  return createPortal(children, newWindow.document.body);
};

render(<App />, document.getElementById("root"));
2 of 4
1

This worked for me with React 18.

    const secondaryWindow = window.open('', '_blank', 'width=600,height=400');
    secondaryWindow.document.open();

    secondaryWindow.document.write(`
    <html>
        <head>
            <title>My Title</title>
        </head>
        <body>
            <h2>My Header</h2>
            <div id="scheduling-grid"></div>
        </body>
    </html>
    `);

    const secondaryRoot = secondaryWindow.document.getElementById('scheduling-grid');

    const secondaryRootInstance = createRoot(secondaryRoot);

    secondaryRootInstance.render(<RelevantComponent prop={prop} />)

🌐
GitHub
github.com › axelmy318 › react-window-open
GitHub - axelmy318/react-window-open: A customizable component that opens a new window using the window.open API
A customizable component that opens a new window using the window.open API ... import React, { useState } from 'react' import { NewWindow } from 'react-window-open' const Example = () => { const [isOpen, setIsOpen] = useState(false) const [counter, ...
Author   axelmy318
🌐
GitHub
github.com › bartlomiejzuber › use-open-window
GitHub - bartlomiejzuber/use-open-window: React hook to open links in separate windows ✔️
import { useOpenInWindow } from 'use-open-window'; const options = { url: 'https://www.google.com/' /* url to page to open */ centered: true, /* default */ security: { noopener: true, /* default */ noreferrer: true, /* default */ nofollow: false /* default */ }, specs: { width: 800, /* window width */ height: 600, /* window height */ } }; const App = () => { const [handleWindowOpen, newWindowHandle] = useOpenInWindow(); return ( <div className="App"> <div onClick={(ev) => handleWindowOpen(ev, options)}>Click me</div> </div> ); } export default App;
Starred by 13 users
Forked by 5 users
Languages   HTML 46.0% | TypeScript 31.5% | JavaScript 13.2% | CSS 9.3% | HTML 46.0% | TypeScript 31.5% | JavaScript 13.2% | CSS 9.3%