Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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>
  );
}
Source by dev.to #
 
PREVIOUS NEXT
Tagged: #History #push #redirecting #page
ADD COMMENT
Topic
Name
2+8 =