简体   繁体   English

如何延迟反应应用程序的加载直到触发动作?

[英]How to delay a react app from loading until a action is triggered?

I am working on a web application project. 我正在从事一个Web应用程序项目。 The project consists of normal php web pages and an embedded react app. 该项目包括普通的php网页和嵌入式的react应用程序。 I have developed all the web pages and outsourced the react app section. 我已经开发了所有网页,并将React App部分外包。

I have put the react app in an iframe. 我已将React应用放入iframe。

• I want to delay the react app from uploading until everything on my web page is loaded. •我想延迟React应用程序的上传,直到加载网页上的所有内容为止。

This is because I am sending information in to the app via iframe.contentWindow.postMessage() . 这是因为我正在通过iframe.contentWindow.postMessage()将信息发送到应用程序中。 And I am using an eventlistener to get the information. 我正在使用事件监听器来获取信息。

The problem I am experiencing is that react is loaded before the event is triggered. 我遇到的问题是在触发事件之前加载了react。 So my react act keeps reading an undefined source. 因此,我的反应行为一直在读取未定义的来源。 So I want to delay it from loading until after either the web page is loaded or after the event is triggered. 因此,我想将它的加载延迟到网页加载完毕或事件触发之后。

• I cannot block the iframe from loading cause the iframe.contentWindow.postMessage() posts the information to the iframe on load. •我无法阻止iframe加载,因为iframe.contentWindow.postMessage()在加载时iframe.contentWindow.postMessage()信息发布到iframe。 Can anyone show me how to delay the react app from loading until the post message is sent? 谁能告诉我如何延迟React应用的加载,直到发送发布消息?

Thanks in advance guys 在此先感谢大家

I would allow the React app to load on page load, but have it display a "loading..." spinner. 我允许React应用程序在页面加载时加载,但是让它显示“ loading ...”微调器。 Add a property to the React component called "isLoaded" which is initially set to false. 向React组件添加一个名为“ isLoaded”的属性,该属性最初设置为false。 When the data / web page is ready to render, set isLoaded to true and the React app will re-render with the actual content. 当数据/网页准备好渲染时,将isLoaded设置为true,React应用将使用实际内容重新渲染。

  constructor(props) {
    super(props);
    this.state = {
      isLoaded: false,
    }
  }

  componentDidMount() {
    // wait until the data is loaded, then set isLoaded to true
    window.addEventListener("someevent", function() {
      this.setState({isLoaded: true});
    }
  }

  render() {
    if (this.state.isLoaded) {
      return (
        // Actual Content
      )
    } else {
      // show loading spinner
    }
  }

You may need to work with your event listener to know when the iframe has sent it's postmessage. 您可能需要与事件侦听器一起使用,以了解iframe何时发送了其后消息。 For instance, maybe there is a hidden element somewhere that is both aware of the postmessage and is listened to by the above React component. 例如,也许某个地方有一个隐藏的元素,它既知道后消息,又被上述React组件侦听。

Here's a post that gives more options on getting React to wait to render. 这是一篇文章,提供了更多让React等待渲染的选项。 React - Display loading screen while DOM is rendering? 反应-在渲染DOM时显示加载屏幕吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM