Minimal Example: Opening a window in electron from react application?

后端 未结 3 1447
花落未央
花落未央 2021-01-05 14:48

Say I am building a desktop application with react/redux & electron. So my index.html file in electron looks like this:




        
3条回答
  •  花落未央
    2021-01-05 15:26

    The "createPortal" api in react 16 will help you.

    First let's say we have a component like this:

    
      

    bar

    Then open(or close) a window in its lifecycle methods like this:

    export class SubWindow extends React.Component {
      nativeWindowObject: null;
    
      componentWillMount() {
        this.nativeWindowObject = window.open('');
      }
      render() {
        return this.nativeWindowObject ? ReactDOM.createPortal(this.props.children, this.nativeWindowObject.document.body) : null;
      }
    }
    

    Notice: You MUST set webPreferences:{nativeWindowOpen: true} in your main window to make sure "window.open" returns a native Window object. Visit https://electronjs.org/docs/api/window-open for more infomation.

提交回复
热议问题