久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

最小示例:從反應(yīng)應(yīng)用程序中打開電子窗口?

Minimal Example: Opening a window in electron from react application?(最小示例:從反應(yīng)應(yīng)用程序中打開電子窗口?)
本文介紹了最小示例:從反應(yīng)應(yīng)用程序中打開電子窗口?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

假設(shè)我正在使用 react/redux & 構(gòu)建一個(gè)桌面應(yīng)用程序.電子.所以我在電子中的 index.html 文件看起來像這樣:

<!DOCTYPE html><html>...<身體><div id="content"></div><script src="public/js/bundle.js"></script></身體></html>

我最大的 React 容器(稱為 app.js)被加載到 'id=content' div 中.到目前為止這工作正常,但現(xiàn)在我想知道當(dāng)用戶單擊我的反應(yīng)應(yīng)用程序中的按鈕時(shí)如何打開一個(gè)新的文件對(duì)話框窗口(或任何新窗口).

我發(fā)現(xiàn)了一些例子here &這里,但是這兩個(gè)示例都只說明了如何從主電子進(jìn)程(在渲染器或主進(jìn)程中)加載文件對(duì)話框窗口.

但是,我希望用戶與我的 React 應(yīng)用程序互動(dòng),然后,一旦他或她點(diǎn)擊一個(gè)按鈕,我的應(yīng)用程序應(yīng)該告訴電子產(chǎn)生一個(gè)新窗口,這個(gè)新窗口當(dāng)然應(yīng)該以某種方式成為一部分我的反應(yīng)應(yīng)用程序.

如果有人能在這里提供一個(gè)最小的例子,說明這些如何協(xié)同工作,我將不勝感激.

解決方案

點(diǎn)擊按鈕在新窗口中打開 React 組件并檢測(cè)窗口何時(shí)關(guān)閉 因?yàn)榻M件不會(huì)簡(jiǎn)單地調(diào)用 componentWillUnmount 當(dāng)你關(guān)閉窗口時(shí) 你的應(yīng)用應(yīng)該是這樣的

App.js

導(dǎo)出默認(rèn)類 App 擴(kuò)展 React.Component {構(gòu)造函數(shù)(道具){超級(jí)(道具);這個(gè).state = {//跟蹤新窗口是否打開或關(guān)閉isNewWindow:假,};}使成為() {返回(//onClose 將在新窗口關(guān)閉時(shí)觸發(fā)//NewWindowComponent 中的所有內(nèi)容都被認(rèn)為是 props.children 并且將是//在新窗口中顯示{(this.state.isNewWindow) &&<新窗口組件onClose={() =>this.setState({ isNewWindow: false })><h2>這將顯示在一個(gè)新窗口中</h2></新窗口組件>}<按鈕onClick={() =>this.setState({ isNewWindow: true })}>在新窗口中打開</按鈕>)}}

NewWindowComponent.js

導(dǎo)出默認(rèn)類 NewWindowComponent extends Component {//創(chuàng)建一個(gè)容器 

對(duì)于窗戶private containerEl = document.createElement('div');//這將保留窗口的引用私人外部窗口=空;//當(dāng)組件掛載時(shí),打開一個(gè)新窗口組件DidMount() {//window.open 中的第二個(gè)參數(shù)是可選的,可以設(shè)置為任意一個(gè)//你想要的值.當(dāng)我們修改 main 時(shí)你會(huì)注意到這個(gè)值的使用//electron.js 文件this.externalWindow = window.open('', 'NewWindowComponent ');//附加容器 div 并注冊(cè)將觸發(fā)的事件//窗口關(guān)閉如果(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 或者你的主電子文件被命名

<代碼>...函數(shù)創(chuàng)建窗口(){主窗口 = 新瀏覽器窗口({...//你需要激活 `nativeWindowOpen`webPreferences: { nativeWindowOpen: true },});...mainWindow.webContents.on('新窗口',(事件、url、frameName、disposition、options、additionalFeatures)=>{//這是我們?yōu)榇翱谶x擇的名稱.你可以有多個(gè)名字//多個(gè)窗口,每個(gè)窗口都有自己的選項(xiàng)if (frameName === 'NewWindowComponent') {event.preventDefault();Object.assign(選項(xiàng),{//這將阻止與主窗口的交互父:主窗口,寬度:300,身高:300,//你也可以設(shè)置 `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);
    }
  });
  ...
}
...

這篇關(guān)于最小示例:從反應(yīng)應(yīng)用程序中打開電子窗口?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進(jìn)程中創(chuàng)建子窗口時(shí)如何修復(fù) BrowserWindow 不是構(gòu)造函數(shù)錯(cuò)誤) - IT屋-程序員軟件開發(fā)技術(shù)
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應(yīng)用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個(gè) loadURL 上執(zhí)行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內(nèi)使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲(chǔ)?)
How to access camera/webcamera inside electron app?(如何在電子應(yīng)用程序中訪問相機(jī)/網(wǎng)絡(luò)攝像頭?)
主站蜘蛛池模板: 九九九视频精品 | 日韩1区 | 国产福利二区 | 狠狠操在线| 欧美成人一级 | 国产有码 | 99在线精品视频 | 日韩一区二区久久 | 亚洲人成网亚洲欧洲无码 | 午夜看电影在线观看 | 91久久伊人 | 你懂的av| 国产精品欧美一区二区三区不卡 | 成人在线影视 | 精品成人在线观看 | 欧美精品久久久 | 国产精品永久免费视频 | 国产美女自拍视频 | 黄色一级大片视频 | 国产高清视频在线观看 | 欧美一区二区黄 | 欧美一区二区三区在线观看 | 欧美va大片 | 日韩精品视频在线播放 | 一区二区三区av | 成人av一区二区三区 | 一区二区在线 | 亚洲精选一区 | 久久精品国产一区二区三区 | 中文字幕高清免费日韩视频在线 | 日韩免费看片 | 国产一区在线免费观看视频 | 免费黄色网址视频 | 久草热8精品视频在线观看 午夜伦4480yy私人影院 | 大陆一级毛片免费视频观看 | 在线看h| 色播久久 | 国产婷婷综合 | 精品国产免费人成在线观看 | 精品视频一区二区三区在线观看 | 国产91丝袜在线播放 |