问题描述
假设我正在使用 react/redux & 构建一个桌面应用程序.电子.所以我在电子中的 index.html 文件看起来像这样:
<!DOCTYPE html><html>...<身体><div id="content"></div><script src="public/js/bundle.js"></script></身体></html>我最大的 React 容器(称为 app.js)被加载到 'id=content' div 中.到目前为止这工作正常,但现在我想知道当用户单击我的反应应用程序中的按钮时如何打开一个新的文件对话框窗口(或任何新窗口).
我发现了一些例子here &这里,但是这两个示例都只说明了如何从主电子进程(在渲染器或主进程中)加载文件对话框窗口.
但是,我希望用户与我的 React 应用程序互动,然后,一旦他或她点击一个按钮,我的应用程序应该告诉电子产生一个新窗口,这个新窗口当然应该以某种方式成为一部分我的反应应用程序.
如果有人能在这里提供一个最小的例子,说明这些如何协同工作,我将不胜感激.
点击按钮在新窗口中打开 React 组件并检测窗口何时关闭 因为组件不会简单地调用 componentWillUnmount 当你关闭窗口时 你的应用应该是这样的
App.js
导出默认类 App 扩展 React.Component {构造函数(道具){超级(道具);这个.state = {//跟踪新窗口是否打开或关闭isNewWindow:假,};}使成为() {返回(//onClose 将在新窗口关闭时触发//NewWindowComponent 中的所有内容都被认为是 props.children 并且将是//在新窗口中显示{(this.state.isNewWindow) &&<新窗口组件onClose={() =>this.setState({ isNewWindow: false })><h2>这将显示在一个新窗口中</h2></新窗口组件>}<按钮onClick={() =>this.setState({ isNewWindow: true })}>在新窗口中打开</按钮>)}}NewWindowComponent.js
导出默认类 NewWindowComponent extends Component {//创建一个容器 对于窗户private containerEl = document.createElement('div');//这将保留窗口的引用私人外部窗口=空;//当组件挂载时,打开一个新窗口组件DidMount() {//window.open 中的第二个参数是可选的,可以设置为任意一个//你想要的值.当我们修改 main 时你会注意到这个值的使用//electron.js 文件this.externalWindow = window.open('', 'NewWindowComponent ');//附加容器 div 并注册将触发的事件//窗口关闭如果(this.externalWindow){this.externalWindow.document.body.appendChild(this.containerEl);this.externalWindow.onunload = () =>this.props.onClose();}}使成为() {返回 ReactDOM.createPortal(this.props.children, this.containerEl);}}electron-main.js 或者你的主电子文件被命名
<代码>...函数创建窗口(){主窗口 = 新浏览器窗口({...//你需要激活 `nativeWindowOpen`webPreferences: { nativeWindowOpen: true },});...mainWindow.webContents.on('新窗口',(事件、url、frameName、disposition、options、additionalFeatures)=>{//这是我们为窗口选择的名称.你可以有多个名字//多个窗口,每个窗口都有自己的选项if (frameName === 'NewWindowComponent') {event.preventDefault();Object.assign(选项,{//这将阻止与主窗口的交互父:主窗口,宽度:300,身高:300,//你也可以设置 `left` 和 `top` 的位置});event.newGuest = new BrowserWindow(options);}});...}...Say I am building a desktop application with react/redux & electron. So my index.html file in electron looks like this:
<!DOCTYPE html>
<html>
...
<body>
<div id="content"></div>
<script src="public/js/bundle.js"></script>
</body>
</html>
My biggest React container (call it app.js) is loaded into the 'id=content' div. This works fine so far, but now I am wondering how to open a new file dialog window (or any new window for that matter) when the user clicks a button in my react application.
I found some examples here & here, but both examples only explain how to load the file dialog window from the main electron processes (in renderer or main).
However, I want the user to engage with my React Application and then, once he or she clicks a button, my app should then tell electron to spawn a new window, and this new window should, of course, somehow be part of my react application.
I would really appreciate it if someone could provide a minimal example here, on how these to things work together.
解决方案 To open a react component in a new window on button click and detect when the window is closed because the component will not simply call componentWillUnmount when you close the window Your app should look like this
App.js
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
// To keep track of the new window if opened or closed
isNewWindow: false,
};
}
render() {
return(
// onClose will be fired when the new window is closed
// everything inside NewWindowComponent is considered props.children and will be
// displayed in a new window
{(this.state.isNewWindow) &&
<NewWindowComponent
onClose={() => this.setState({ isNewWindow: false })>
<h2>This will display in a new window</h2>
</NewWindowComponent> }
<button
onClick={() => this.setState({ isNewWindow: true })}>
Open in new window</button>
)
}
}
NewWindowComponent.js
export default class NewWindowComponent extends Component {
// Create a container <div> for the window
private containerEl = document.createElement('div');
// This will keep a reference of the window
private externalWindow = null;
// When the component mounts, Open a new window
componentDidMount() {
// The second argument in window.open is optional and can be set to whichever
// value you want. You will notice the use of this value when we modify the main
// electron.js file
this.externalWindow = window.open('', 'NewWindowComponent ');
// Append the container div and register the event that will get fired when the
// window is closed
if (this.externalWindow)
{
this.externalWindow.document.body.appendChild(this.containerEl);
this.externalWindow.onunload = () => this.props.onClose();
}
}
render() {
return ReactDOM.createPortal(this.props.children, this.containerEl);
}
}
electron-main.js or however your main electron file is named
...
function createWindow() {
mainWindow = new BrowserWindow({
...
// You need to activate `nativeWindowOpen`
webPreferences: { nativeWindowOpen: true },
});
...
mainWindow.webContents.on('new-window',
(event, url, frameName, disposition, options, additionalFeatures) =>
{
// This is the name we chose for our window. You can have multiple names for
// multiple windows and each have their options
if (frameName === 'NewWindowComponent ') {
event.preventDefault();
Object.assign(options, {
// This will prevent interactions with the mainWindow
parent: mainWindow,
width: 300,
height: 300,
// You can also set `left` and `top` positions
});
event.newGuest = new BrowserWindow(options);
}
});
...
}
...
这篇关于最小示例:从反应应用程序中打开电子窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
首先,我们需要在百度地图开放平台上申请一个开发者账号,并创建一个应用。在创建应用的过程中,我们会得到一个密钥(ak),这是调用API的凭证。 接下来,我们需要准备一个PHP文件,以便可以在网页中调用。首先,我们需要引入百度地图API的JS文件,代码如下...
2024-11-22
前端开发问题
244
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22
前端开发问题
182
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18
前端开发问题
301
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17
前端开发问题
437
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11
前端开发问题
455
问题描述: 我需要在我的应用程序中验证一个文本字段。它既不能包含数字,也不能包含特殊字符,所以我尝试了这个正则表达式:/[a-zA-Z]/匹配,问题是,当我在字符串的中间或结尾放入一个数字或特殊字符时,这个正则表达式依然可以匹配通过。 解决办法: 你应...
2024-06-06
前端开发问题
165
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)