Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
How to navigate (redirect) a user to a page after registering the user - React

I want to redirect a user to a page that informs the user to check his email for verification immediately after registration. I am trying to use Navigate from react-router-dom but the return statement was not being executed. Below is my code

API call

import { ToastNotification } from "../components/ToastNotification";
import axiosInstance from "../utils/axiosInstance";
import { Navigate } from "react-router-dom";

export const userRegister = async (email, password, regd_as) => {
  const body = JSON.stringify({ email, password, regd_as });

  try {
    const response = await axiosInstance.post(`/account/signup/`, body);
    if (response.status === 201) {
      ToastNotification("Account has been created successfully.", "success");
      return <Navigate to="/verify-email" />;  //this is not being executed
    }
  } catch (error) {
    if (error.response) {
      if (
        error.response.status === 400 &&
        error.response.data.message === "User Exists"
      ) {
        ToastNotification("Account already exists.", "error");
      } else {
        ToastNotification(
          "We are facing some problems. Please try registering later.",
          "error"
        );
      }
    } else if (error.request) {
      ToastNotification(
        "We are facing some problems. Please try registering some time later."
      );
    }
  }
};


component calling the API

  const formDataSubmit = (e) => {
    e.preventDefault();
    if (password !== conPassword) {
      ToastNotification(
        "Password and Confirm Password did not match.",
        "error"
      );
    } else {
      if (regd_as === "") {
        ToastNotification(`"Registered As" cannot be blank`, "info");
      } else {
        userRegister(email, password, regd_as);
      }
    }
  };

what should I do to redirect the user immediately after registering?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can't return JSX from a callback like this and expect it to be rendered and affect any change. In other words, the Navigate component isn't actually rendered into the DOM to do anything.

Use the useNavigate hook to access a navigate function as part of a resolved Promise from the userRegister function. Pass the navigate function to the handler.

Example:

import { useNavigate } from 'react-router-dom';

...

const navigate = useNavigate();

...

const formDataSubmit = (e) => {
  e.preventDefault();
  if (password !== conPassword) {
    ToastNotification(
      "Password and Confirm Password did not match.",
      "error"
    );
  } else {
    if (regd_as === "") {
      ToastNotification(`"Registered As" cannot be blank`, "info");
    } else {
      userRegister(email, password, regd_as, navigate); // <-- pass navigate function
    }
  }
};

...

import { ToastNotification } from "../components/ToastNotification";
import axiosInstance from "../utils/axiosInstance";

export const userRegister = async (email, password, regd_as, navigate) => {
  const body = JSON.stringify({ email, password, regd_as });

  try {
    const response = await axiosInstance.post(`/account/signup/`, body);
    if (response.status === 201) {
      ToastNotification("Account has been created successfully.", "success");
      return navigate("/verify-email" { replace: true }); // <-- issue imperative redirect
    }
  } catch (error) {
    if (error.response) {
      if (
        error.response.status === 400 &&
        error.response.data.message === "User Exists"
      ) {
        ToastNotification("Account already exists.", "error");
      } else {
        ToastNotification(
          "We are facing some problems. Please try registering later.",
          "error"
        );
      }
    } else if (error.request) {
      ToastNotification(
        "We are facing some problems. Please try registering some time later."
      );
    }
  }
};
about 4 years ago · Juan Pablo Isaza Relatório

0

I think the return should be wrapped with brackets like this

 return(
   <>
 <Navigate to="/verify-email" />
   </>
)

Here's more info https://www.pluralsight.com/guides/return-variable-in-render-function-in-react

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda