Answers for "history.redirect react"

6

react router redirect

<Route exact path="/">
  {loggedIn ? <Redirect to="/dashboard" /> : <PublicHomePage />}
</Route>
Posted by: Guest on May-17-2020
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 "Javascript"

Browse Popular Code Answers by Language