reactjs - 如何告诉我的mainWindow在另一个窗口关闭后重新加载?

标签 reactjs redux electron

我正在开发需要Twitch OAuth的应用程序。在我的主窗口中,我有一个按钮,然后打开另一个BrowserWindow并使用以下命令打开登录Twitch所需的URL:
Login.tsx

import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { Redirect } from 'react-router-dom';
import Spinner from 'react-bootstrap/Spinner';
import routes from '../constants/routes.json';
import styles from './Login.css';
import Config from '../lib/Config';
import {
  enableLoading,
  isLoading
} from '../features/login/loginSlice';

export default function Login() {
  const dispatch = useDispatch();
  const loading = useSelector(isLoading);
  const config = new Config();
  if(config.getAccessToken()) {
    return <Redirect to={routes.COUNTER}/>;
  }
  return (
    <div>
      {loading ? <div>Waiting for Twitch authentication...<div><Spinner animation="border"/></div></div> : <button onClick={() => dispatch(enableLoading())} className={styles.loginButton}><i className='fab fa-lg fa-twitch'/>CONNECT WITH TWITCH</button>}
    </div>
  );
}
loginSlice.ts (所有Redux内容)
import { createSlice } from '@reduxjs/toolkit';
import { RootState } from '../../store';
const electron = require('electron');
import { BrowserWindow } from 'electron';
const BW = electron.remote.BrowserWindow;
import Config from '../../lib/Config';

const config = new Config();

let authWindow: BrowserWindow | null = null;

const loginSlice = createSlice({
  name: 'login',
  initialState: { loading: false },
  reducers: {
    enableLoading: (state) => {
      state.loading = true;
      const scopes = ['user:read:email', 'moderation:read'];
      const clientID = 'wq852hkxpa4tpfu1anqr5h5gvtncwa';
      const scope = scopes.join('+');
      const url = `https://id.twitch.tv/oauth2/authorize?response_type=token&client_id=${clientID}&redirect_uri=http://localhost/callback&scope=${scope}`;
      authWindow = new BW({
        show: true,
        width: 1024,
        height: 728,
        webPreferences: {
          nodeIntegration: true,
        },
      });
      authWindow.loadURL(url);
      authWindow.webContents.on('did-start-navigation', (_event, url) => {
        processCallbackUrl(url);
      });

      authWindow.webContents.on('will-redirect', (_event, url) => {
        processCallbackUrl(url);
      });
      authWindow.on('closed', () => {
        authWindow?.destroy();
      });
    },
    disableLoading: (state) => {
      state.loading = false;
    },
  },
});

function processCallbackUrl(url: string) {
  var token = /#access_token=(\w+)/.exec(url) || null;
  if (token) {
    config.setAccessToken(token[1]);
    authWindow?.destroy();
    //HERE IS WHERE I NEED TO TELL MY PAGE TO REDIRECT OR RELOAD
  }
}

export const { enableLoading, disableLoading } = loginSlice.actions;

export default loginSlice.reducer;

export const isLoading = (state: RootState) => state.login.loading;
所有这些都很好。除了现在,我需要loginSlice.ts以某种方式告诉主窗口(已加载Login.tsx)重定向到其他地方。
有什么办法可以做到这一点?

最佳答案

  const ipcRenderer = electron.remote.ipcRenderer;

  authWindow.on('closed', () => {
    authWindow?.destroy();
    ipcRenderer.send('reload');
  });
在创建mainWindow的主要过程中
  ipcMain.on('reload', (e) => {
    mainWindow.webContents.executeJavaScript("window.reload()");
  })

关于reactjs - 如何告诉我的mainWindow在另一个窗口关闭后重新加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63606139/

相关文章:

javascript - Redux - 如何在其提交处理程序中获取表单值并调用 event.preventDefault()?

javascript - 匹配的路由在路由哈希更改时不会更改

javascript - 如何将这三个函数全部写在父组件中,而不是写在子组件中 - ReactJS

java - 发布开关输入 boolean 值(React/Typescript)

javascript - Babel 在箭头函数上抛出语法错误

reactjs - Redux表格: initialValues does not set form values

node.js - 我无法使用node.js运行 Electron

javascript - 是否可以将远程文件从 Electron App 拖到文件系统上?

typescript - 类型不可分配(TypeScript 4.1.2)

reactjs - 如何自定义 React Native TextInput 的高度?