国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

登錄后使用React react-router和useNavigate,為什么重定向到主頁(yè)沒(méi)有效果?
P粉555696738
P粉555696738 2023-08-26 22:34:52
0
1
567
<p>成功登錄后,我希望用戶(hù)被重定向到主頁(yè),我將用戶(hù)登錄的api請(qǐng)求數(shù)據(jù)存儲(chǔ)在一個(gè)名為currentUser的變量中,所以如果currentUser為true,它應(yīng)該重定向到主頁(yè)。 這是我處理登錄請(qǐng)求的方式:</p> <pre class="brush:php;toolbar:false;">export const AuthContextProvider = ({ children }) => { const [currentUser, setCurrentUser] = useState( JSON.parse(localStorage.getItem("users")) || null ); const login = async (inputs) => { const res= await axios.post("https://micacarballo-social-media-api.onrender.com/api/v1/auth/login",inputs,{ }) setCurrentUser(res.data) }; useEffect(() => { localStorage.setItem("users", JSON.stringify(currentUser)); }, [currentUser,login]); const updateUser = async () =>{ const user = JSON.parse(localStorage.getItem("users")) const res= await axios.get("https://micacarballo-social-media-api.onrender.com/api/v1/users/me/",{ headers: { Authorization: `jwt ${user.token}` } }) setCurrentUser(res.data) } return ( <AuthContext.Provider value={{ currentUser, login , updateUser}}> {children} </AuthContext.Provider> ); };</pre> <p>我的受保護(hù)的路由:</p> <pre class="brush:php;toolbar:false;">import { AuthContext } from './context/authContext' function App() { const { currentUser } = useContext(AuthContext); const Layout = ()=>{ return( <div > <Navbar /> <div style={{ display: "flex" }}> <LeftBar /> <div style={{ flex: 6 }}> <Outlet /> </div> </div> </div> ) } const ProtectedRoute = ({children}) =>{ 如果(!當(dāng)前用戶(hù)){ return <導(dǎo)航至=“/login” >> }返回孩子 } const 路由器 = createBrowserRouter([ { 路徑:“/”, 元素: ( <受保護(hù)的路由> <布局/> </受保護(hù)的路由> ), 孩子們 :[ { 路徑:“/”, 元素:<首頁(yè)/> },{ 路徑:“/profile/:id”, 元素:<配置文件/>> } ] }, { 路徑:“/登錄”, 元素:<登錄/>, }, { 路徑:“/注冊(cè)”, 元素:<注冊(cè)/> }, ]); 返回 (
) } 導(dǎo)出默認(rèn)應(yīng)用程序 <p>我的登錄頁(yè)面:</p>
const Login = () =>; {
  const [輸入,setInputs] = useState({
    電子郵件:“”,
    密碼:“”,
  });
  const [err, setErr] = useState(null);
  const [isLoading, setIsLoading] = useState(false); // 用于加載微調(diào)器的狀態(tài)變量


  常量導(dǎo)航 = useNavigate()

  const handleChange = (e) =>; {
    setInputs((prev) => ({ ...prev, [e.target.name]: e.target.value }));
  };
  const {登錄} = useContext(AuthContext)

  const handleLogin = async (e) =>; {
    e.preventDefault();
    嘗試 {
      setIsLoading(true); // 設(shè)置加載為 true
      等待登錄(輸入);
      導(dǎo)航(“/”)
      
      
     
    } 捕獲(錯(cuò)誤){
      setErr(err.response.data);
    }最后 {
      setIsLoading(假); // 將加載設(shè)置回 false
    }
  };
       

    返回 (
      
;

SocialMica

<p> 讓與朋友的聯(lián)系變得輕松有趣 </p> <span>還沒(méi)有帳戶(hù)?</span> <鏈接到=“/注冊(cè)”> <按鈕>注冊(cè)
; <div className="right"> <h1>Login</h1> <form action=""> <input type="email" placeholder='email' name='email' onChange={handleChange}></input> <input type="password"placeholder='password' name='password' onChange={handleChange}/> {err && "invalid email or password"} <button onClick={handleLogin} disabled={isLoading}> {isLoading ? <Loading/> : "Login"}</button> </form> </div> </div> </div> ) } export default Login</pre> <p>我不明白為什么它不起作用,用戶(hù)登錄并且currentUser存儲(chǔ)了數(shù)據(jù)后,登錄頁(yè)面只會(huì)刷新而不會(huì)重定向,我必須手動(dòng)訪(fǎng)問(wèn)主頁(yè)。 我嘗試在登錄頁(yè)面的handleSubmit函數(shù)中將navigate更改為("/register"),它可以正常工作,但不能使用navigate('/')</p>
P粉555696738
P粉555696738

全部回復(fù)(1)
P粉797855790

改變你的路徑 /home 不僅僅是 / 就像

{
    path:"/home",
    element: <Home />
},

它應(yīng)該正常工作

最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板