【问题标题】:electron + react + redux communication between two windows freezes PC?两个窗口之间的电子 + 反应 + redux 通信冻结 PC?
【发布时间】:2023-03-16 20:18:01
【问题描述】:

我正在尝试根据第一个窗口中的状态更改来更新另一个窗口中的状态,但是 ipc 通信对我来说没有按预期工作。

在我的第一个窗口中,我有:

onStatsSelect(event, menuItem, index) {
    const selectedStatsCopy = this.state.selectedStats.slice();
    const itemIndex = selectedStatsCopy.indexOf(menuItem.props.primaryText);
    if (itemIndex > -1) {
      delete selectedStatsCopy[itemIndex];
    } else {
      selectedStatsCopy[index] = menuItem.props.primaryText;
    }

    // Update state
    this.setState({ selectedStats: selectedStatsCopy });

    // Notify worker window of change
    if (!(this.props.isReport)) {
      console.log('in renderer main window');
      ipcRenderer.send("updateSelectedStatsMain", event, selectedStatsCopy);
    }
  }

这是一个用于更新 selectedStats 状态的回调。如最后几行所示,它还将通知工作人员窗口此更新。 if (!(this.props.isReport)) 这是一项重要的检查,因为两个窗口共享相同的组件,因此我使用属性 isReport 来区分两者。

在我的主要过程中,我有:

  // Selected Stats have changed in main window
  ipcMain.on('updateSelectedStatsMain', (event, selectedStatsCopy) => {
    console.log('in main');
    // Send to worker window
    workerWindow.webContents.send('updateSelectedStatsRen', event, selectedStatsCopy);
  });

此代码将使用新状态selectedStatsCopy 与工作窗口通信。

在我的comoponentDidMount 中,我有:

componentDidMount() {
    // Register listening to message in case this is the worker window
    if (this.props.isReport) {
      console.log('in renderer worker window');
      ipcRenderer.on("updateSelectedStatsRen", (event, selectedStatsCopy) => {
        console.log('in renderer worker window event');
        this.setState({ selectedStats: selectedStatsCopy });
      });
    }
  }

这应该可以工作,但电子在ipcRenderer.send("updateSelectedStatsMain", event, selectedStatsCopy); 行挂起,使主窗口挂起一段时间,并继续使用资源,直到 PC 冻结。

这里有什么问题?

【问题讨论】:

  • 某处可能存在循环,例如您尝试在渲染方法中设置状态,这会导致重新渲染循环。寻找类似的东西。
  • 我想我明白了。传入方法的参数不是我所期望的。
  • 不,不是这样。仍然无法解决这个问题:(

标签: reactjs electron


【解决方案1】:

我发现了我仍然不知道它为什么冻结电子的错误。

基本上我在做

ipcRenderer.send("updateSelectedStatsMain", event, selectedStatsCopy);

这完全没有意义,因为我将事件作为参数传递。我什至没有要传递的事件变量。

更新:

ipcRenderer.send("updateSelectedStatsMain",event, selectedStatsCopy);

对此:

ipcRenderer.send("updateSelectedStatsMain", selectedStatsCopy);

还有这个:

workerWindow.webContents.send('updateSelectedStatsRen', event, selectedStatsCopy);

对此:

workerWindow.webContents.send('updateSelectedStatsRen', selectedStatsCopy);

为我解决了这个问题。

【讨论】:

    猜你喜欢
    • 2017-03-08
    • 2021-03-22
    • 2018-07-01
    • 2013-10-21
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 2016-11-19
    相关资源
    最近更新 更多