Answers for "how to redirect on somepage using route.push in react"

1

function that redirects to another page react

const navigate = useNavigate();
const goToLoginPage = () => navigate('/login');
Posted by: Guest on October-09-2021
0

History push for redirecting to another page in react-router v6

import React from 'react';

const LocationContext = React.createContext();

export default function Router({ children }) {
  return (
    <LocationContext.Provider
      value={{
        // The current location
        location: window.location,
        navigator: {
          // Change url and push entry in the history
          push(to) {
            window.history.pushState(null, null, to);
          },
          // Change url and replace the last entry in the history
          replace(to) {
            window.history.replaceState(null, null, to);
          },
          // Go back to the previous entry in the history
          back() {
            window.history.go(-1);
          },
          // Go forward to the next entry in the history
          forward() {
            window.history.go(1);
          },
          // If we want to go forward or 
          // backward from more than 1 step
          go(step) {
            window.history.go(step);
          }
        }
      }}
    >
      {children}
    </LocationContext.Provider>
  );
}
Posted by: Guest on March-13-2022

Code answers related to "how to redirect on somepage using route.push in react"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language