O
O
Orion Orion2022-01-19 11:15:45
JavaScript
Orion Orion, 2022-01-19 11:15:45

How to make a page transition in react-router-dom?

I do authorization, send a request with data, a token arrives in localstorage, everything works fine (almost), but does not go to the desired page (in this case, Main)

import React from 'react'
import { Routes, Route, Navigate } from 'react-router-dom'
import Home from './component/Home'
import Main from './component/Main'


 export const useRoutes = (isLogin) => {
     if(isLogin){
          return (
           <Routes>
                      <Route path="/login" element={<Main/>}/>
            </Routes>
         )
     }
    
         return (
        <Routes>
            <Route path="/" element={<Home/>}/>
            <Route path="*" element={<Navigate to="/"/>}/>
        </Routes>
         )
     
}


It needs to go to the Main page if isLogin=true

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
I Phoenix I, 2022-01-19
@Harlock

Use the useNavigate
method Example:

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

function SignupForm() {
  let navigate = useNavigate();

  async function handleSubmit(event) {
    event.preventDefault();
    await submitForm(event.target);
    navigate("../success", { replace: true });
  }

  return <form onSubmit={handleSubmit}>{/* ... */}</form>;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question