简体   繁体   English

如何确保页面已在 React 站点上完成加载

[英]How to ensure that page has finished loading on a React site

Is there a way to make sure that a web page, coded in React, has finished loading?有没有办法确保用 React 编码的网页已经完成加载? The usual javascript tricks, don't seem to work for React, meaning that you cannot use safely these:通常的 javascript 技巧似乎不适用于 React,这意味着您不能安全地使用这些技巧:

document.readyState == 'complete' && jQuery.active == 0 document.readyState == '完成' && jQuery.active == 0

We are working on automated tests and because of this, we cannot have access to client side code, so we cannot or should create callbacks etc, in order to get the info we want.我们正在进行自动化测试,因此,我们无法访问客户端代码,因此我们不能或应该创建回调等,以获得我们想要的信息。 The easy way is to use thread sleep, but this is very unreliable, frustrating, and bad practice.简单的方法是使用线程睡眠,但这是非常不可靠、令人沮丧和不好的做法。 Is there another way, like executing a js script that will return such information?还有另一种方法,例如执行将返回此类信息的js脚本吗?

Is there a way to make sure that a web page, coded in React, has finished loading?有没有办法确保以React编码的网页已完成加载? The usual javascript tricks, don't seem to work for React, meaning that you cannot use safely these:通常的JavaScript技巧似乎不适用于React,这意味着您不能安全地使用以下这些技巧:

document.readyState == 'complete' && jQuery.active == 0 document.readyState =='complete'&& jQuery.active == 0

We are working on automated tests and because of this, we cannot have access to client side code, so we cannot or should create callbacks etc, in order to get the info we want.我们正在从事自动化测试,因此,我们无法访问客户端代码,因此我们无法或应该创建回调等以获取所需信息。 The easy way is to use thread sleep, but this is very unreliable, frustrating, and bad practice.最简单的方法是使用线程睡眠,但这是非常不可靠,令人沮丧且不明智的做法。 Is there another way, like executing a js script that will return such information?还有另一种方法,例如执行将返回此类信息的js脚本吗?

Is there a way to make sure that a web page, coded in React, has finished loading?有没有办法确保以React编码的网页已完成加载? The usual javascript tricks, don't seem to work for React, meaning that you cannot use safely these:通常的JavaScript技巧似乎不适用于React,这意味着您不能安全地使用以下这些技巧:

document.readyState == 'complete' && jQuery.active == 0 document.readyState =='complete'&& jQuery.active == 0

We are working on automated tests and because of this, we cannot have access to client side code, so we cannot or should create callbacks etc, in order to get the info we want.我们正在从事自动化测试,因此,我们无法访问客户端代码,因此我们无法或应该创建回调等以获取所需信息。 The easy way is to use thread sleep, but this is very unreliable, frustrating, and bad practice.最简单的方法是使用线程睡眠,但这是非常不可靠,令人沮丧且不明智的做法。 Is there another way, like executing a js script that will return such information?还有另一种方法,例如执行将返回此类信息的js脚本吗?

Just in case someone needs this...以防万一有人需要这个...

The approach below is the best approach to solve your problem.以下方法是解决您的问题的最佳方法。

function Component() {
    const [loaded, setStatus] = useState(false);

    document.onreadystatechange = () => {
        setStatus(document.readyState === "complete");
    }
    return {loaded ? <Loaded />: <Loading />};
}

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

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