簡體   English   中英

組件在 componentWillUpdate 或 componentDidUpdate 中重復調用 setState。 React 限制了編號。 嵌套更新以防止無限循環

[英]component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the no. of nested updates to prevent infinite loops

我收到此錯誤:“超出最大更新深度。當組件在 componentWillUpdate 或 componentDidUpdate 中重復調用 setState 時,可能會發生這種情況。React 限制嵌套更新的數量以防止無限循環。”

調用demoPortalPage 中的history.push("/")時,有時會無限循環並無限次設置路徑。

錯誤截圖:錯誤截圖

演示門戶頁面.jsx

useEffect(() => {
    const authenticateDemoUser = async () => {
        const authData = {
            email: 'demo@gmail.com',
            password: '1234',
        };

        const checkUserAuth = await postRequest("authenticate", authData);

        if (checkUserAuth?.status >= 200 && checkUserAuth?.status < 300) {
            // sign in success action call
                await signInSuccess(checkUserAuth.data);
                await getAndSaveUserName();
                await setIsUserVerified(true);
                // localStorage.setItem('is_demo', "true")
                history.push("/");
            
        } else {
             await setIsUserVerified(false);
             history.push("/");
        }
    }
    authenticateDemoUser()

}, [])

應用程序.jsx

 ....   
    <Route exact path={"/login"} render={() => (isUserVerified ? <Redirect to="/" /> : <LoginPage/>)} />
                               
    <Route exact path={"/demo-login"} component={DemoPortalPage} />
 ....

請幫我解決這個錯誤

它已解決,我想從我的案例角度為您澄清這件事......在我的案例中,它是由於異步序列而發生的...... 在將數據保存在 state 之前。頁面重定向到另一個頁面,下一頁沒有獲取該數據,。! 根據我的路由代碼,如果頁面沒有特定數據,它會在循環中一次又一次地檢查,這就是無限循環錯誤發生的原因。

謝謝你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM