【问题标题】:React 360 render to location from componentReact 360 从组件渲染到位置
【发布时间】:2018-05-09 14:29:28
【问题描述】:

我的样板中有两个组件,其中一个我只想在从另一个组件调用方法时挂载。

我该怎么做?

这是我的样板:

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};

这是我的组件:

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

  mountNewComponent() {
    //HOW TO MOUNT COMPONENT2 FROM HERE??
  }
  ..
}

【问题讨论】:

    标签: reactjs react-360


    【解决方案1】:

    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
      }
    }
    

    在组件中:

    mountNewComponent() {    
      postMessage({ type: "newComponent"});
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-19
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 2017-12-17
      • 1970-01-01
      • 2020-12-12
      • 2017-03-08
      相关资源
      最近更新 更多