Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!