Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

reactjs - 在React中关闭外部窗口(Close external Window in React)

I can not seem to close an external Window in React, I have the following HOC:

(我似乎无法在React中关闭外部窗口,我有以下HOC:)

export const WindowContext = createContext<IWindowContext | null>(null);

export const WindowProvider: FC = ({ children }) => {
  const [win, setWin] = useState<IWindowContext["win"]>(null);

  const contextValue = useMemo(
    () => ({
      win,
      setWin
    }),
    [win, setWin]
  );

  return (
    <WindowContext.Provider value={contextValue}>
      {children}
    </WindowContext.Provider>
  );
};

This is how I am opening the window:

(这就是我打开窗户的方式:)

onClick={() => {
  const openedWindow = window.open(
    `${process.env.REACT_APP_SERVER}/auth/login`,
    "targetWindow",
    "width=500,height=500"
  );

  setWin(openedWindow);
}}

When the login fails, I am redirecting the window back to my Home component from my server using res.redirect (express) but I can not seem to get any logs in the external window or be able to close it

(登录失败时,我正在使用res.redirect (express)将窗口从服务器重定向回我的Home组件,但是我似乎无法在外部窗口中获取任何日志或能够关闭它)

// Does not log in the external window, the window's url is localhost:3000/dashboard?error=access_denied...
export const Home = () => {
  const location = useLocation();

  const { win } = useWindow();

  const searchParams = new URLSearchParams(location.search);

  if (win && searchParams.has("error")) {
    console.log(true);
    win.close();
  }
  ask by Daniell translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...