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 :: use jquery in project using NPM 
Javascript :: jq get value without quotes 
Javascript :: for loop js 
Javascript :: javascript querySelector change input value 
Javascript :: redux devtools config 
Javascript :: how to target html elements in javascript 
Javascript :: convert UTC date to Indonesian local date format 
Javascript :: Ping discord 
Javascript :: mongoose in nodem js 
Javascript :: js if text contains lowercase 
Javascript :: access shadow root element 
Javascript :: filter js object array based on multiple parameters 
Javascript :: how to set asp radio button value to checked as per retrieve record in javascript 
Javascript :: export json to excel in javascript 
Javascript :: html get input box value by element id 
Javascript :: convert timestamp to date js 
Javascript :: how to add two floats 
Javascript :: js how to find max value in an array 
Javascript :: exec in node js 
Javascript :: 10 javascript interview questions 
Javascript :: how to use msg.send instead of msg.reply discord.js javascript 
Javascript :: make a function and return the index of specific character in javascript 
Javascript :: json to yaml converter 
Javascript :: filter function using recursion 
Javascript :: node js postgresql query 
Javascript :: The element.onclick Property 
Javascript :: rngesturehandlermodule.default.direction react native 
Javascript :: google translate javascript 
Javascript :: check if alpine js is activated in website 
Javascript :: js map delete item 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =