未重定向至所需链接

问题描述 投票:0回答:1

问题是,每当我尝试结账时,如果我没有登录,它会转到链接

"http://localhost:3000/login?redirect=shipping"
登录,然后转到必须输入地址等的运输网站。

它重定向到

"http://localhost:3000/login/shipping"
但它应该重定向到
"http://localhost:3000/shipping"

代码是:

import React, { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Link, useLocation, useNavigate } from "react-router-dom";
import { login } from "../../redux/action/userAction";

const Login = () => {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");

  const location = useLocation();
  const navigate = useNavigate();

  const redirect = location.search ? location.search.split("=")[1] : "/";
  console.log(`Login Redirect ${redirect}`); // here it take "shipping" from redirect variable

  const dispatch = useDispatch();
  const userLogin = useSelector((state) => state.userLogin);

  const { loading, error, userInfo } = userLogin;
  // dispatch(login({ email, password }));

  useEffect(() => {
    if (userInfo) {
      navigate(redirect);
    }
  }, [userInfo, redirect]);

  const sumbitHandler = (e) => {
    e.preventDefault();
    dispatch(login(email, password));
  };

  return (
    <div className="flex flex-col justify-center mx-6 my-6">
      <div className="hidden"></div>
      <div className="flex flex-col gap-10">
        {error && (
          <h1 className="text-center bg-red-500 text-red-600 text-sm py-4 w-full">
            {error}
          </h1>
        )}
        <div className="flex flex-col gap-6">
          <h1 className="text-3xl font-medium">Log In to Exclusive</h1>
          <p className="text-sm">Enter your details below</p>
        </div>
        <div className="flex flex-col gap-4">
          <form onSubmit={sumbitHandler} autoComplete="off">
            <div className="flex flex-col gap-6">
              <div>
                <input
                  type="email"
                  placeholder="Enter Email"
                  value={email}
                  onChange={(e) => setEmail(e.target.value)}
                  className="text-sm border-b-[1px] border-black/[60%] w-full px-1 py-3"
                  autoComplete="off"
                />
              </div>
              <div>
                <input
                  type="password"
                  placeholder="Password"
                  value={password}
                  onChange={(e) => setPassword(e.target.value)}
                  className="text-sm border-b-[1px] border-black/[60%] w-full px-1 py-3"
                  autoComplete="off"
                />
              </div>
              <div className="my-">
                <button
                  className="text-base text-white bg-black py-4 w-full rounded-full"
                  type="submit"
                >
                  Login
                </button>
>               </div>
            </div>
          </form>
          <div>
            <h1 className="text-center text-xs text-black/[60%]">
              Don't have an account?{" "}
              <span className="hover:underline">
                <Link
                  to={redirect ? `/signup?redirect=${redirect}` : "/signup"}
                >
                  SingUp
                </Link>
              </span>
            </h1>
          </div>
        </div>
      </div>
    </div>
  );
};

export default Login;

我尝试进行手动导航,例如提供指向

"/shipping"
的直接路径,但效果不佳。

javascript reactjs react-hooks react-router react-router-dom
1个回答
0
投票

您正在“重定向”到

"shipping"
,这是一个相对路径,因为它不以前导
"/"
字符开头。相对路径将被append到当前路径,例如如果当前路径是
"/login"
,则
navigate("shipping")
将导航到
"/login/shipping"

读取

redirect
搜索参数并确保它是具有前导
"/"
的绝对路径,例如
"/shipping"

const Login = () => {
  ...

  const navigate = useNavigate();
  const [searchParams] = useSearchParams();

  const redirect = `/${searchParams.get("redirect") ?? ""}`; // "/shipping"
  
  ...

  useEffect(() => {
    if (userInfo) {
      navigate(redirect, { replace: true });
    }
  }, [userInfo, redirect]);
© www.soinside.com 2019 - 2024. All rights reserved.