REACT JS窗口.加载前无法正常工作



我需要根据获取代码状态的响应执行一个操作。如果我得到代码状态200,window.beforeunload应该可以工作。否则,加载前的窗口不应工作。

这是我的代码`function DialogueEdition(props({

let codeStatusGeneral = 200;
const handleFetch = async () => {
    try {        
        const maReponse = await fetch(`a string`)
            .then(function (response) {
                codeStatusGeneral = response.status;
                return response;
            })
        if (codeStatusGeneral === 200) {
            console.log(403)            
            const recuperation = await maReponse.json();                
        }
        if (codeStatusGeneral === 403) {
            console.log(403)
        }
        if (codeStatusGeneral === 400) {
            console.log("I am DEAD")
        }
        
    } catch (e) {
        
    } finally {
         console.log("finish")
    }
}
handleFetchRefresh = ()=>{
 window.location.reload();
}
if (codeStatusGeneral===200) {
    window.addEventListener("beforeunload", function (e) {
        //many actions
        for (var i = 0; i < 500000; i++) { }
        return undefined;
    });
return (
        <>
            <Button onClick={handleFetch}>
                Fetch
            </Button>
            <Button onclick={handleFetchRefresh}>
                Refresh
            </Button>
        
        </>
    )

}`

我做了多个console.log,我注意到在刷新期间codeStatusGeneral在200中进行了转换。因此,它总是验证我的";如果";。但这并不是我所期望的。请帮帮我。提前谢谢。真诚地

回复我自己,在初始化过程中,我们不能直接将if条件与值一起使用。否则,它将取第一个值而不取更改

最新更新