【问题标题】:Electron: Change React component state from main.jsElectron:从 main.js 更改 React 组件状态
【发布时间】:2018-05-06 13:14:38
【问题描述】:

我有一个包含(除其他外)以下文件的 Electron 应用程序:

  • index.js
  • browserwindow.html
  • browserwindow.js(从 browserwindow.jsx 编译而来)

index.js 是 Electron 应用程序启动时运行的主要 Electron/Node 进程。 browserwindow.htmlguiWindow 中呈现,browserwindow.js 管理这个窗口。 (请参阅下面的文件。)

我想要的是从主 Electron 进程发送一条 ipc 消息到browserwindow.js,然后更新 React 组件状态。但是当我使用下面文件中的代码并运行 Electron 应用程序时,状态并没有改变。

index.js

const electron = require('electron');
const BrowserWindow = electron.BrowserWindow;
const app = electron.app;
const url = require('url');
const ipc = electron.ipcMain;


app.on('ready', function(){

  // Make window
  var guiWindow;

  // Set size and do not immediately show
  guiWindow = new BrowserWindow({
    width: 800,
    height: 780,
    show: false
  });

  // Load browserwindow.html in the guiWindow
  guiWindow.loadURL('file://' + __dirname + '/browserwindow.html');

  // Show the window when the .html file is loaded
  guiWindow.once('ready-to-show', function(){
    guiWindow.show();
  });

  // Send an ipc after 3 seconds
  setInterval(function(){
    guiWindow.webContents.send('message', {msg: 'Hello World!'});
  }, 3000);

});

browserwindow.html

<!DOCTYPE html>
<html>
  <head>
    <title>Page</title>
  </head>
  <body>
    <!-- The div that React uses: -->
    <div id="mainInterface"></div>
    <script src="react-0.14.3.js"></script>
    <script src="react-dom-0.14.3.js"></script>
    <script src="browserwindow.js"></script>
  </body>
</html>

编译成browserwindow.js的jsx

var electron = require('electron');
var shell = electron.shell;
var ipc = electron.ipcRenderer;

class MainInterface extends React.Component {

  constructor(props, contect){
    super(props);
    this.state = {
      testValue: 'Initial state'
    };
  };

  componentDidMount(){ // When the document is rendered

    ipc.on('message', function(event, data){ // When the message is received...
      console.log('Message received');
      this.setState({testValue: 'It worked!'}); // ... change the state of this React component
    });

  }

  render(){
    return (
      <h1>{ this.state.testValue }</h1>
    );
  }

}

ReactDOM.render(
  <MainInterface />,
  document.getElementById('mainInterface')
);

在 Javascript 控制台中,我收到以下错误:

Uncaught TypeError: this.setState is not a function
    at EventEmitter.<anonymous> (file:///C:/Users/<file path to the project on my computer>/testproject/browserwindow.js:20:12)
    at emitTwo (events.js:106:13)
    at EventEmitter.emit (events.js:194:7)

我可以做些什么来解决这个问题?

(对于某些背景,我正在制作一个 Electron 应用程序,它应该通过 MQTT 接收消息并根据接收到的消息更新屏幕上的元素。)

【问题讨论】:

  • 您是否将React 导入到browserwindow.js 中?
  • @JosanIracheta 我在browserwindow.html 中导入它,在&lt;script src="browserwindow.js"&gt;&lt;/script&gt; 之前使用&lt;script src="react-0.14.3.js"&gt;&lt;/script&gt;&lt;script src="react-dom-0.14.3.js"&gt;&lt;/script&gt;
  • 注释掉ipc.on('message'...,看看this.setState是否在它之外工作

标签: javascript node.js reactjs electron


【解决方案1】:

您的this 指向与组件实际不同的上下文。您需要将代码更改为

componentDidMount() {
    // When the document is rendered.
    const self = this;
    ipc.on('message', function (event, data) {
        // When the message is received...
        console.log('Message received');
        // ... change the state of this React component.
        self.setState({testValue: 'It worked!'});
    });
}

或者您可以将function() 回调替换为() =&gt; {},因为第一个选项会更改执行的上下文。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 2020-04-07
    • 2018-09-21
    • 1970-01-01
    相关资源
    最近更新 更多