adrianhajdin / project_next_14_ai_prompt_sharing

Next.js recently became the official React framework as outlined in React docs. In this course, you'll learn the most important Next.js concepts and how they fit into the React ecosystem. Finally, you'll put your skills to the test by building a modern full-stack Next 14 application.
https://www.jsmastery.pro/ultimate-next-course
2.9k stars 427 forks source link

NextRouter was not mounted. #110

Closed theterrificm closed 1 month ago

theterrificm commented 1 month ago

Getting this error despite running router inside my page component.

app\profile\page.jsx

'use client'
import { useState, useEffect } from 'react'
import { useSession } from 'next-auth/react'
import { useRouter } from 'next/router'

import Profile from '@/src/components/Profile'

const MyProfile = () => {

  const {data: session} = useSession();
  const router = useRouter();

  const [posts, setPosts] = useState([])

  useEffect(()=>{

    const fetchPosts = async ()=>{
      const response = await fetch(`/api/users/${session?.user.id}/posts`)
      const data = await response.json()

      setPosts(data)
    }

    if (session?.user.id){
      fetchPosts()
    } 

  }, [session?.user.id]) 

  const handleEdit = (post) => {

    router.push(`/update-prompt?id=${post._id}`)

  }

  const handleDelete = async (post) => {

  }

  return (
    <Profile 
      name="My"
      desc="Welcome to your personalized profile page"
      data={posts}
      handleEdit={handleEdit}
      handleDelete={handleDelete}
    /> // Profile
  )
}

export default MyProfile

Tried everything, any help would be appreciated.

theterrificm commented 1 month ago

Can't get that. Why?

theterrificm commented 1 month ago

My bad the problem was with my import statement.

The correct statement is: import { useRouter } from 'next/navigation'

And i was importing with import { useRouter } from 'next/router'