Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

next js back to previous page

import { useRouter } from 'next/router'

export default function Page() {
  const router = useRouter()

  return (
    <button type="button" onClick={() => router.back()}>
      Click here to go back
    </button>
  )
}
Comment

how to go back to previous route in next.js

//if user not logged in
if (!auth.user) {
  window.localStorage.setItem("path", router.asPath); //save current route
  router.replace("/login"); //send user to login page
  return <div> redirecting to login... </div>
}

//when user login
if (auth.user) {
  const path = localStorage.getItem("path")
  router.replace(path || "/"); //navigate to last route
  return <div> Loading... </div>
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js enum 
Javascript :: json multiple records 
Javascript :: javascript vue.js right click 
Javascript :: laravel data return in json 
Javascript :: how to run bare react-native project 
Javascript :: react native tab.screen hide title 
Javascript :: type of data model mongodb 
Javascript :: return response json two variables laravel 
Javascript :: how to add toaster in angular 9 
Javascript :: click anchor tag using jquery 
Javascript :: dangerouslySetInnerHTML 
Javascript :: javascript does not equal 
Javascript :: how to get dropdown selected value in javascript onchange 
Javascript :: check url with javascript 
Javascript :: javascript WeakSet Methods 
Javascript :: js window location relative path 
Javascript :: regexp constructor javascript 
Javascript :: how to stop google colab from disconnecting 
Javascript :: remove comma from number javascript 
Javascript :: deleting key of json object 
Javascript :: javascript create array of objects from multiple arrays 
Javascript :: how to read json file in python stack overflow 
Javascript :: mongoose increment sub document 
Javascript :: javascript url check 
Javascript :: how to remove duplicates in array in javascript 
Javascript :: get date in javascript 
Javascript :: cannot get issue in nodejs 
Javascript :: regular expression twitter user 
Javascript :: joi object id validation 
Javascript :: javascript cors error 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =