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");
}}
Answer from HudsonGraeme on Stack Overflow
🌐
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 ✔️
Note: Version ^1.4.0 of this hook has dependency on React 18, if you wish to use it with React 17 then please use version ^1.3.0. ... import { useOpenInWindow } from 'use-open-window'; const url = 'https://www.google.com/'; const options = { 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(url, options); return ( <div className="App"> <div onClick={handleWindowOpen}>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-window-open
react-window-open - npm
January 18, 2023 - Latest version: 1.1.0, last published: 3 years ago. Start using react-window-open in your project by running `npm i react-window-open`. There are no other projects in the npm registry using react-window-open.
      » 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 › rmariuzzo › react-new-window
GitHub - rmariuzzo/react-new-window: 🔲 Pop new windows in React, using `window.open`.
import React from 'react' import NewWindow from 'react-new-window' const Demo = () => ( <NewWindow> <h1>Hi 👋</h1> </NewWindow> ) When <NewWindow /> is mounted a popup window will be opened.
Starred by 464 users
Forked by 108 users
Languages   JavaScript 98.4% | HTML 1.6%
Find elsewhere
🌐
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
🌐
npm
npmjs.com › package › react-new-window
react-new-window - npm
November 27, 2022 - import React from 'react' import NewWindow from 'react-new-window' const Demo = () => ( <NewWindow> <h1>Hi 👋</h1> </NewWindow> ) When <NewWindow /> is mounted a popup window will be opened.
      » npm install react-new-window
    
Published   Nov 27, 2022
Version   1.0.1
Author   Rubens Mariuzzo
🌐
GitHub
github.com › JakeGinnivan › react-popout
GitHub - JakeGinnivan/react-popout: React popout is a React component wrapping window.open allowing you to host content in a browser popup window.
React popout is a React component wrapping window.open allowing you to host content in a browser popup window. - JakeGinnivan/react-popout
Starred by 192 users
Forked by 56 users
Languages   TypeScript 93.6% | JavaScript 6.4% | TypeScript 93.6% | JavaScript 6.4%
🌐
CodeSandbox
codesandbox.io › examples › package › react-window-open
react-window-open examples - CodeSandbox
Use this online react-window-open playground to view and fork react-window-open example apps and templates on CodeSandbox.
🌐
npm
npmjs.com › package › react-popout
react-popout - npm
React popout is a React component wrapping window.open allowing you to host content in a browser popup window.
      » npm install react-popout
    
Published   Dec 31, 2024
Version   4.0.0
Author   Jake Ginnivan
🌐
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
🌐
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
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);
};
🌐
GitHub
github.com › ahakem › react-window.opener
GitHub - ahakem/react-window.opener · GitHub
import WindowOpener from 'react-window-opener' export default function Example { const childResponse = (err, res) => { if (err) { console.log(res, 'err') } console.log(res, 'res') } return ( <WindowOpener url='/popUp-URL' bridge={childResponse}> ) }
Starred by 14 users
Forked by 2 users
Languages   JavaScript 68.6% | HTML 25.5% | CSS 5.9%
🌐
npm
npmjs.com › package › react-window-opener
react-window-opener - npm
June 14, 2020 - Latest version: 0.0.7, last published: 6 years ago. Start using react-window-opener in your project by running `npm i react-window-opener`. There are no other projects in the npm registry using react-window-opener.
      » npm install react-window-opener
    
Published   Jun 14, 2020
Version   0.0.7
Author   ahakem
🌐
Hacker News
news.ycombinator.com › item
Using React 16 to open new window with shared state | Hacker News
November 14, 2017 - That was I can abstract it into convenience classes for connecting, event listening, etc · You have three projects/clients/orders/enquiries/purchase orders/invoices/etc. You want to compare them/discuss them/etc
🌐
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 24, 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.