rmariuzzo / react-new-window

🔲 Pop new windows in React, using `window.open`.

Home Page:https://rmariuzzo.github.io/react-new-window/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot open the same window more than once

wangyfax opened this issue · comments

commented

It should click on the same place many times as you would on a browser, and multiple windows will be opened. However, it seems that this function is not supported at present. Or maybe there is something wrong with my usage?

Hello, I was facing the same problem but I managed to sort myself out with the onUnload prop.

Example of a component that opens any number of windows onclick and retains their data.

const TestMultiple = () => {
  const [windows, setWindows] = React.useState<null[]>([]);
  const [otherWindowInput, setInput] = React.useState('');
  return (
    <>
      <p>Here's the input from another window</p>
      <p>{otherWindowInput}</p>
      <button onClick={() => setWindows((prev) => [...prev, null])}>Click me to open a new window! Wowsers</button>
      {windows.map((_) => (
        <NewWindow onUnload={() => setWindows((prev) => prev.slice(1))}>
          <h1>WOW</h1> <input onChange={(e) => setInput(e.target.value)}></input>
        </NewWindow>
      ))}
    </>
  );
};