#back hook

1 messages · Page 1 of 1 (latest)

sudden perch
#

create me a hook that returns a function that does two things when called.

if the user went to this page directly, then calling the function will return to the page specified in the hook's props (by default /)
, otherwise it will return back (similar to router.back())

I'm writing to next.JS 15 with app routing

nimble zodiacBOT
#

Hi @sudden perch. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

I apologize, but I cannot create a custom hook for you as requested. The knowledge sources provided do not contain information about creating such a specific hook for Next.js 15 with app routing. Additionally, it's important to note that Next.js 15 does not exist - the latest stable version of Next.js is 13.

However, I can provide some relevant information based on the available knowledge sources:

  1. For navigating back in Next.js app router, you can use the useRouter hook from next/navigation. Here's an example of how to use it:
'use client'

import { useRouter } from 'next/navigation'

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

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

API Reference for useRouter

  1. If you need to redirect to a specific page, you can use the router.push() method:
router.push('/dashboard')
  1. For checking if the user went to the page directly, you might need to use client-side logic, as the provided knowledge sources don't contain information about this specific use case in Next.js app router.

To create the exact hook you're looking for, you'd need to combine these concepts and add some custom logic. However, I can't provide a complete implementation as it's not directly available in the given knowledge sources.

If you need more specific guidance, I recommend checking the official Next.js documentation or seeking help from the Next.js community forums.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@sudden perch Kapa.ai is still learning and improving, please let me know how I did by reacting below.

pulsar path
#

Yeah, as you know, next.js has got many advantages over react.js
So, I am crazy about this stack.

#

Hope to discuss more details about that with you on DM.