useEffect钩子用于通过本地存储登录。

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

useEffect hook for login via local storage

问题

  1. //模块
  2. import { useState, useEffect } from "react";
  3. import { useRouter } from "next/navigation";
  4. //样式
  5. import styles from "./page.module.sass";
  6. //组件
  7. import NewCV from "./components/newCv";
  8. import Cvs from "./components/cvs";
  9. import PersonalInfo from "./components/personalInfo";
  10. import LayoutPicker from "./components/LayoutPicker";
  11. //帮助器
  12. import { vaildToken } from "@/app/authorization";
  13. export default function MyPage() {
  14. const router = useRouter();
  15. const [catType, setCatType] = useState(0);
  16. const cat = (type) => setCatType(type);
  17. const [valid, setValid] = useState(false);
  18. const [role, setRole] = useState('')
  19. useEffect(() => {
  20. const res = vaildToken(localStorage.getItem("token"));
  21. setValid(res[0]);
  22. setRole(res[1]);
  23. if (valid !== true && role !== "USER") {
  24. router.push("/login");
  25. }
  26. }, [valid, role]);
  27. console.log(valid, role)
  28. return (
  29. <main className={styles.main}>
  30. {valid !== true && role !== "USER" ? (
  31. <div className={styles.container}>
  32. <LayoutPicker cat={cat} />
  33. {catType == 1 ? <Cvs /> : catType == 2 ? <NewCV /> : <PersonalInfo />}
  34. </div>
  35. ) : (
  36. <></>
  37. )}
  38. </main>
  39. );
  40. }
英文:

I'm creating a React + Next.js app with authorisation and I need to get token from local storage. If I do this without useEffect, there is an error of undefined localstorage, but with use effect my redirects don't work, maybe you know where am I wrong?

  1. &quot;use client&quot;;
  2. //modules
  3. import { useState, useEffect } from &quot;react&quot;;
  4. import { useRouter } from &quot;next/navigation&quot;;
  5. //styles
  6. import styles from &quot;./page.module.sass&quot;;
  7. //components
  8. import NewCV from &quot;./components/newCv&quot;;
  9. import Cvs from &quot;./components/cvs&quot;;
  10. import PersonalInfo from &quot;./components/personalInfo&quot;;
  11. import LayoutPicker from &quot;./components/LayoutPicker&quot;;
  12. //helpers
  13. import { vaildToken } from &quot;@/app/authorization&quot;;
  14. export default function MyPage() {
  15. const router = useRouter();
  16. const [catType, setCatType] = useState(0);
  17. const cat = (type) =&gt; setCatType(type);
  18. const [valid, setValid] = useState(false);
  19. const [role, setRole] = useState(&#39;&#39;)
  20. useEffect(() =&gt; {
  21. const res = vaildToken(localStorage.getItem(&quot;token&quot;));
  22. setValid(res[0]);
  23. setRole(res[1]);
  24. if (valid !== true &amp;&amp; role !== &quot;USER&quot;) {
  25. router.push(&quot;/login&quot;);
  26. }
  27. }, [valid, role]);
  28. console.log(valid, role)
  29. return (
  30. &lt;main className={styles.main}&gt;
  31. {valid !== true &amp;&amp; role !== &quot;USER&quot; ? (
  32. &lt;div className={styles.container}&gt;
  33. &lt;LayoutPicker cat={cat} /&gt;
  34. {catType == 1 ? &lt;Cvs /&gt; : catType == 2 ? &lt;NewCV /&gt; : &lt;PersonalInfo /&gt;}
  35. &lt;/div&gt;
  36. ) : (
  37. &lt;&gt;&lt;/&gt;
  38. )}
  39. &lt;/main&gt;
  40. );
  41. }

答案1

得分: 1

更改条件如下,因为您不会立即获得状态值。

  1. if (res[0] !== true && res[1] !== "USER") {
  2. router.push("/login");
  3. }
  4. 希望它能工作
英文:

Change the condition as follows cause you won't get the state value immediately.

  1. if (res[0] !== true &amp;&amp; res[1] !== &quot;USER&quot;) {
  2. router.push(&quot;/login&quot;);
  3. }

Hope, it'll work.

huangapple
  • 本文由 发表于 2023年7月13日 23:07:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680916.html
匿名

发表评论

匿名网友

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

确定