Electron: Change React component state from main.js(电子:将Reaction组件状态从main.js更改)
本文介绍了电子:将Reaction组件状态从main.js更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个电子应用程序,其中包括以下文件:
index.js
browserwindow.html
browserwindow.js
(从browserwindow.jsx
编译而来)
index.js
是电子应用程序启动时运行的主电子/节点进程。browserwindow.html
在guiWindow
中呈现,browserwindow.js
管理此窗口。(请参见下面的文件。)
browserwindow.js
发送一条IPC消息,然后更新Reaction组件状态。但是,当我使用下面文件中的代码并运行电子应用程序时,状态不会更改。
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接收消息,并根据收到的消息更新屏幕上的元素。)
推荐答案
您的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()
回调替换为() => {}
,因为第一个选项更改了执行的上下文。
这篇关于电子:将Reaction组件状态从main.js更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:电子:将Reaction组件状态从main.js更改


基础教程推荐
猜你喜欢
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 动态更新多个选择框 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 在for循环中使用setTimeout 2022-01-01