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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…