0

I have two components in my boilerplate, one of which I would like to mount only when a method is called from the other component.

How could I do this?

Here is my boilerplate:

import {ReactInstance} from 'react-360-web';

function init(bundle, parent, options = {}) {
  ...
  // Render 1
  r360.renderToSurface(
    r360.createRoot('Component1'),
    r360.getDefaultSurface()
  );

  //I WOULD LIKE TO MOUNT THIS COMPONENT LATER!
  r360.renderToLocation(
    r360.createRoot('Component2'),
    r360.getDefaultLocation(),
  );
}

window.React360 = {init};

Here is my component:

export default class Component1 extends React.Component {
  constructor(props) {
    super(props)
  }

  mountNewComponent() {
    //HOW TO MOUNT COMPONENT2 FROM HERE??
  }
  ..
}
bear
  • 663
  • 1
  • 14
  • 33

1 Answers1

0

client.js:

function init(bundle, parent, options = {}) {
  ...
  r360.runtime.executor._worker.addEventListener('message', (e) => onMessage(e, r360));
}

function onMessage(e, r360) {
  if(e.data.type === 'newComponent') {
    //TODO mount hier
  }
}

in the component:

mountNewComponent() {    
  postMessage({ type: "newComponent"});
}
Jan F.
  • 1,681
  • 2
  • 15
  • 27