警告:更新深度超过最大限制

huangapple go评论59阅读模式
英文:

Warning: Maximum update depth exceeded

问题

在我的 app.js 文件中,我想要检查用户是否已登录,如果是,将其重定向到选择的URL。如果没有登录,我想要自动重定向到 /connexion URL(登录页面)。如果用户已登录,然后我想要检查用户的个人资料是否已完善,如果是,它应该将我重定向到所选的URL,否则我希望它将我重定向到完整信息页面 /information

我以前只有一个检查已登录的检查,但是当我添加了信息检查后,我开始出现错误。我知道错误来自于我的 useEffect,以及它在每个检查中不断切换,但是我不确定如何修复它。

以下是我的代码:

const App = () => {
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  const [tokenChecked, setTokenChecked] = useState(false);
  const { profile } = useStateContext();
  
  useEffect(() => {
    const token = sessionStorage.getItem("token");
    setIsLoggedIn(token ? true : false);
    setTokenChecked(true);
  }, []);

  return (
    <Router>
      {tokenChecked ? (
        <Routes>
          <Route element={<NavbarLayout />}>
            <Route path="/" exact element={<HomePage />} />
            <Route path="/about" exact element={<AboutPage />} />
            <Route path="/how-it-works" exact element={<HowItWorks />} />
          </Route>
          <Route element={<NoNavbarLayout />}>
            <Route path="/connexion" exact element={<UserLogin />} />
            <Route path="/récupérer" exact element={<UserRecovery />} />
            <Route path="/register-user" exact element={<UserRegister />} />
            <Route
              path="/register-tech"
              exact
              element={<TechnicianRegister />}
            />
            <Route path="/register-admin" exact element={<AdminRegister />} />
          </Route>
          <Route element={<ConfirmLayout />}>
            <Route
              path="/confirmation"
              exact
              element={<AccountConfirmation />}
            />
            <Route path="/nonconnecté" exact element={<UserRedirectLMsg />} />
            <Route
              path="/validation/:id/:confirm"
              exact
              element={<AccountValidation />}
            />
          </Route>
          {isLoggedIn === true ? (
            <>
            {profile === 'finished' ? (
                <>
                  <Route element={<ConfirmLayout />}>
                    <Route path="/encours" exact element={<UserPending />} />
                    <Route
                      path="/information"
                      exact
                      element={
                        <Elements stripe={stripePromise}>
                          <UserInfo />
                        </Elements>
                      }
                    />
                  </Route>
                  <Route element={<DashboardLayout />}>
                    <Route
                      path="/nouvel-appareil"
                      exact
                      element={<UserNewDevice />}
                    />
                    <Route path="/new-project" exact element={<NewProject />} />
                    <Route
                      path="/demande-contrat"
                      exact
                      element={<NewContract />}
                    />
                    <Route path="/user-dash" exact element={<UserDash />} />
                    <Route path="/profile" exact element={<UserProfile />} />
                    <Route
                      path="/user-history"
                      exact
                      element={<UserHistory />}
                    />
                    <Route path="/navigation" exact element={<Compte />} />

                    <Route
                      path="/user-notification"
                      exact
                      element={<UserNotification />}
                    />
                    <Route
                      path="/user-projets-details/:id"
                      exact
                      element={<UserProjectDetails />}
                    />
                    <Route
                      path="/user-intervention-details/:id"
                      exact
                      element={<InterventionDetail />}
                    />
                    <Route
                      path="/user-details/:id"
                      exact
                      element={<UserDetail />}
                    />
                    <Route
                      path="/user-claim-details/:id"
                      exact
                      element={<UserClaimsDetail />}
                    />
                    <Route
                      path="/reclamations"
                      exact
                      element={<UserClaims />}
                    />
                    <Route path="/browse" exact element={<SearchGrid />} />

                    <Route
                      path="/technician-dash"
                      exact
                      element={<TechnicianDash />}
                    />
                    <Route
                      path="/technician-profile"
                      exact
                      element={<TechnicianProfile />}
                    />
                    <Route
                      path="/technician-projets"
                      exact
                      element={<TechnicianProjects />}
                    />
                    <Route
                      path="/technician-history"
                      exact
                      element={<UserHistory />}
                    />
                    <Route
                      path="/technician-projets-details/:id"
                      exact
                      element={<UserProjectDetails />}
                    />
                    <Route
                      path="/product/:id"
                      exact
                      element={<UserNewProductDetail />}
                    />
                    <Route
                      path="/admin-notification"
                      exact
                      element={<AdminNotification />}
                    />
                    <Route path="/new-user" exact element={<AddUser />} />
                    <Route path="/litiges" exact element={<Litige />} />
                    <Route
                      path="/réclamations"
                      exact
                      element={<Reclamation />}
                    />
                    <Route path="/users" exact element={<UsersList />} />
                  </Route>
                  <Route element={<NoNavbarLayout />}>
                    <Route
                      path="/register-type"
                      element={<Navigate to="/navigation" />}
                    />
                  </Route>
                </>
            ) : (
              <>
              <Route
                path="*"
                element={<Navigate to="/information" replace />}
              />
            </>
            )}
            </>
          ) : isLoggedIn === false ? (
            <>
                <Route
                  path="*"
                  element={<Navigate to="/connexion" replace />}
                />
            </>
          ) : null}
        </Routes>
      ) : null}
    </Router>
  );
};

export default App;

请注意,这是您提供的代码的翻译部分,不包括代码本身。如果您需要更多帮助或有其他问题,请随时提问。

英文:

In my app.js I want to check if first the user is logged in and want it to direct me to the URL picked. If not, I want it to automatically direct me to the /connexion URL (login page) if the user is logged in I want it then to check if the user profile is complete, if so, it should direct me to the picked URL or else I want it to direct me to the complete info page /information

I used to have only one check the logged in check however when I added the information check I started getting I know that the error is coming from my useEffect and the fact it keeps toggling in every check however I am not sure how to fix it:

this is my code:

const App = () =&gt; {
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [tokenChecked, setTokenChecked] = useState(false);
const { profile } = useStateContext();
useEffect(() =&gt; {
const token = sessionStorage.getItem(&quot;token&quot;);
setIsLoggedIn(token ? true : false);
setTokenChecked(true);
}, []);
return (
&lt;Router&gt;
{tokenChecked ? (
&lt;Routes&gt;
&lt;Route element={&lt;NavbarLayout /&gt;}&gt;
&lt;Route path=&quot;/&quot; exact element={&lt;HomePage /&gt;} /&gt;
&lt;Route path=&quot;/about&quot; exact element={&lt;AboutPage /&gt;} /&gt;
&lt;Route path=&quot;/how-it-works&quot; exact element={&lt;HowItWorks /&gt;} /&gt;
&lt;/Route&gt;
&lt;Route element={&lt;NoNavbarLayout /&gt;}&gt;
{/*&lt;Route path=&quot;/register-type&quot; exact element={&lt;LoginPick /&gt;} /&gt;*/}
&lt;Route path=&quot;/connexion&quot; exact element={&lt;UserLogin /&gt;} /&gt;
&lt;Route path=&quot;/r&#233;cup&#233;rer&quot; exact element={&lt;UserRecovery /&gt;} /&gt;
&lt;Route path=&quot;/register-user&quot; exact element={&lt;UserRegister /&gt;} /&gt;
&lt;Route
path=&quot;/register-tech&quot;
exact
element={&lt;TechnicianRegister /&gt;}
/&gt;
&lt;Route path=&quot;/register-admin&quot; exact element={&lt;AdminRegister /&gt;} /&gt;
&lt;/Route&gt;
&lt;Route element={&lt;ConfirmLayout /&gt;}&gt;
&lt;Route
path=&quot;/confirmation&quot;
exact
element={&lt;AccountConfirmation /&gt;}
/&gt;
&lt;Route path=&quot;/nonconnecte&quot; exact element={&lt;UserRedirectLMsg /&gt;} /&gt;
&lt;Route
path=&quot;/validation/:id/:confirm&quot;
exact
element={&lt;AccountValidation /&gt;}
/&gt;
&lt;/Route&gt;
{isLoggedIn === true ? (
&lt;&gt;
{profile === &#39;finished&#39; ? (
&lt;&gt;
&lt;Route element={&lt;ConfirmLayout /&gt;}&gt;
&lt;Route path=&quot;/encours&quot; exact element={&lt;UserPending /&gt;} /&gt;
&lt;Route
path=&quot;/information&quot;
exact
element={
&lt;Elements stripe={stripePromise}&gt;
&lt;UserInfo /&gt;
&lt;/Elements&gt;
}
/&gt;
&lt;/Route&gt;
&lt;Route element={&lt;DashboardLayout /&gt;}&gt;
&lt;Route
path=&quot;/nouvel-appareil&quot;
exact
element={&lt;UserNewDevice /&gt;}
/&gt;
&lt;Route path=&quot;/new-project&quot; exact element={&lt;NewProject /&gt;} /&gt;
&lt;Route
path=&quot;/demande-contrat&quot;
exact
element={&lt;NewContract /&gt;}
/&gt;
&lt;Route path=&quot;/user-dash&quot; exact element={&lt;UserDash /&gt;} /&gt;
&lt;Route path=&quot;/profile&quot; exact element={&lt;UserProfile /&gt;} /&gt;
&lt;Route
path=&quot;/user-history&quot;
exact
element={&lt;UserHistory /&gt;}
/&gt;
&lt;Route path=&quot;/navigation&quot; exact element={&lt;Compte /&gt;} /&gt;
&lt;Route
path=&quot;/user-notification&quot;
exact
element={&lt;UserNotification /&gt;}
/&gt;
&lt;Route
path=&quot;/user-projets-details/:id&quot;
exact
element={&lt;UserProjectDetails /&gt;}
/&gt;
&lt;Route
path=&quot;/user-intervention-details/:id&quot;
exact
element={&lt;InterventionDetail /&gt;}
/&gt;
&lt;Route
path=&quot;/user-details/:id&quot;
exact
element={&lt;UserDetail /&gt;}
/&gt;
&lt;Route
path=&quot;/user-claim-details/:id&quot;
exact
element={&lt;UserClaimsDetail /&gt;}
/&gt;
&lt;Route
path=&quot;/reclamations&quot;
exact
element={&lt;UserClaims /&gt;}
/&gt;
&lt;Route path=&quot;/browse&quot; exact element={&lt;SearchGrid /&gt;} /&gt;
&lt;Route
path=&quot;/technician-dash&quot;
exact
element={&lt;TechnicianDash /&gt;}
/&gt;
&lt;Route
path=&quot;/technician-profile&quot;
exact
element={&lt;TechnicianProfile /&gt;}
/&gt;
&lt;Route
path=&quot;/technician-projets&quot;
exact
element={&lt;TechnicianProjects /&gt;}
/&gt;
&lt;Route
path=&quot;/technician-history&quot;
exact
element={&lt;UserHistory /&gt;}
/&gt;
&lt;Route
path=&quot;/technician-projets-details/:id&quot;
exact
element={&lt;UserProjectDetails /&gt;}
/&gt;
&lt;Route
path=&quot;/product/:id&quot;
exact
element={&lt;UserNewProductDetail /&gt;}
/&gt;
&lt;Route
path=&quot;/admin-notification&quot;
exact
element={&lt;AdminNotification /&gt;}
/&gt;
&lt;Route path=&quot;/new-user&quot; exact element={&lt;AddUser /&gt;} /&gt;
&lt;Route path=&quot;/litiges&quot; exact element={&lt;Litige /&gt;} /&gt;
&lt;Route
path=&quot;/r&#233;clamations&quot;
exact
element={&lt;Reclamation /&gt;}
/&gt;
&lt;Route path=&quot;/users&quot; exact element={&lt;UsersList /&gt;} /&gt;
&lt;/Route&gt;
&lt;Route element={&lt;NoNavbarLayout /&gt;}&gt;
&lt;Route
path=&quot;/register-type&quot;
element={&lt;Navigate to=&quot;/navigation&quot; /&gt;}
/&gt;
&lt;/Route&gt;
&lt;/&gt;
):(
&lt;&gt;
&lt;Route
path=&quot;*&quot;
element={&lt;Navigate to=&quot;/information&quot; replace /&gt;}
/&gt;
&lt;/&gt;              
)}
&lt;/&gt;
) : isLoggedIn === false ? (
&lt;&gt;
&lt;Route
path=&quot;*&quot;
element={&lt;Navigate to=&quot;/connexion&quot; replace /&gt;}
/&gt;
&lt;/&gt;
) : null}
&lt;/Routes&gt;
) : null}
&lt;/Router&gt;
);
};
export default App;

答案1

得分: 2

这个错误是因为您试图渲染具有相同路径的多个路由。

<Route
path="*"
element={<Navigate to="/information" replace />}
/>

它无法访问信息路由,因为个人资料尚未完成。

英文:

This error is caused because you are trying to render multiple Routes with the same path.

 &lt;Route
path=&quot;*&quot;
element={&lt;Navigate to=&quot;/information&quot; replace /&gt;}
/&gt;

It can't access the information route because the profile is not completed yet.

huangapple
  • 本文由 发表于 2023年2月27日 19:41:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75580020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定