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
🌐
GitHub
github.com › rmariuzzo › react-new-window
GitHub - rmariuzzo/react-new-window: 🔲 Pop new windows in React, using `window.open`.
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> )
Starred by 464 users
Forked by 108 users
Languages   JavaScript 98.4% | HTML 1.6%
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);
};
🌐
npm
npmjs.com › package › react-new-window
react-new-window - npm
November 27, 2022 - 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
🌐
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
🌐
CodeSandbox
codesandbox.io › examples › package › react-new-window
react-new-window examples - CodeSandbox
Find more examples or templates · 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 ·
🌐
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 - First like always it’s time to run create-react-app react-popup · Get in that folder and let’s create the file for the component. I called it touch ./src/window-opener.js · Something that we need to clarify first is the fact that by nature browsers don’t allow to open windows, because how we humans like to fill up our sites with Ads and stuff, but it does not matter was we are going 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
React New and Improved Window popup using the window.open API - RyanNerd/react-new-improved-window
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%
🌐
npm
npmjs.com › package › react-window-open
react-window-open - npm
January 18, 2023 - 👌</p> <p>Counter in the new window : {counter}</p> <button onClick={() => setCounter(counter+1)}>Increment from the new window</button> </NewWindow>} <p>Counter on the original page : {counter}</p> <button onClick={() => setCounter(count...
      » npm install react-window-open
    
Published   Jan 18, 2023
Version   1.1.0
Author   Axel Milliery
🌐
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 ...
🌐
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
👌</p> <p>Counter in the new window : {counter}</p> <button onClick={() => setCounter(counter+1)}>Increment from the new window</button> </NewWindow>} <p>Counter on the original page : {counter}</p> <button onClick={() => setCounter(count...
Author   axelmy318
🌐
CodeSandbox
codesandbox.io › examples › package › react-window-open
react-window-open examples - CodeSandbox
react-window-open Sample (forked) bb07110911 · AboutA customizable component that opens a new window using the window.open API56Weekly Downloads · Latest version1.1.0 · LicenseMIT · External Links · github.com/axelmy318/react-window-open#readme · github.com/axelmy318/react-window-open ·
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} />)

🌐
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?
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: ...
🌐
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%
🌐
npm
npmjs.com › package › react-popout
react-popout - npm
// input <Popout containerId='tearoff'> <SomeComponent /> </Popout> // output in new window: <div id="tearoff"> <SomeComponent /> </div> Provides a callback incase the window wasn't opened, usually due to a popout blocker within the browser.
      » npm install react-popout
    
Published   Dec 31, 2024
Version   4.0.0
Author   Jake Ginnivan
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-open-a-component-in-a-new-tab-in-reactjs
How to open a component in a new tab in React JS ? | GeeksforGeeks
August 7, 2024 - "dependencies": { "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.17.0", "react-scripts": "5.0.1", "web-vitals": "^2.1.4", } Example: Let's understand the implementation through example. FirstComponent.js and SecondComponent.js are components designed to open in new tabs upon user clicks, featuring styled headings for enhanced presentation.