2

I am trying to pass a query parameter through my Link href but the query does not seem to come through. After clicking on the edit button I have here, the query I have passes is nowhere to be found. Here is my code. The line to look at is within the asterisks. Thank you in advance.

"use client";
import {auth} from "../../../utils/firebase";
import {useAuthState} from "react-firebase-hooks/auth"; 
import {useRouter} from "next/navigation"; 
import Message from "../components/message";
import { useEffect, useState } from "react";
import { collection, deleteDoc, doc, onSnapshot, query, where} from "firebase/firestore";
import {db} from "../../../utils/firebase";
import {BsTrash2Fill} from 'react-icons/bs';
import {AiFillEdit} from 'react-icons/ai';  
import Link from "next/link";

export default function Dashboard(){
    const route = useRouter();
    const [user,loading] = useAuthState(auth);
    const [posts, setPosts] = useState([]); 
    
    //See if user is logged in 
    const getData = async () => {
        if(loading) return;
        if (!user) return route.push('/auth/login');  
        const collectionRef = collection(db, "posts");
        const q = query(collectionRef, where("user", "==", user.uid));
        const unsubscribe = onSnapshot(q, (snapshot) => {
            setPosts(snapshot.docs.map((doc)=> ({...doc.data(), id: doc.id})));
        });
        return unsubscribe;  
    }
    //Delete Post
    const deletePost = async (id) => {
        const docRef =  doc(db, "posts", id)  
        await deleteDoc(docRef);
    };
    //Get users data
    useEffect(()=>{
        getData();
    },[user,loading]); 
    return(
        <div>
            <h1>Your Posts</h1>
                <div>
                    {posts.map((post)=>{
                        return (<Message key ={post.id} {...post} > <div className="flex gap-4">
                        <button onClick = {()=> deletePost(post.id)}className="text-pink-600 flex items-center justify-center gap-2 py-2 text-sm  "><BsTrash2Fill className="text-2xl "/>Delete</button> 
                        **<Link href = {{pathname: "/post", query: {post}}}><button className="text-teal -600 flex items-center justify-center gap-2 py-2 text-sm  "><AiFillEdit  className="text-2xl "/>Edit</button> </Link>   
                        </div> </Message> 
                    );
                    })}**
                </div>
                <button className= "font-medium text-white bg-gray-800 py-2 px-4 my-6 "onClick={()=> auth.signOut()}>Sign Out</button>
        </div>
    );
};

This is all I get when trying to console.log the router and I can't seem to find the query that I am passing throughconsole log information

6
  • Don't you have a loading loop? Initial effect call will trigger data loading (if user is logged in), and then every change of loading prop will trigger the effect again. Commented Mar 11, 2024 at 18:46
  • Btw. you forgot to return getData result in effect. Having unsubscription functionality, it is enough to have only [user] dependency in useEffect. Then you can safely remove redundant if(loading) return; condition from getData function. Commented Mar 11, 2024 at 18:48
  • Regarding query question, what exactly do you expect to see in query? If all the post params, then you should remove curly brackets, like so: { ..., query: post }, though I'd prefer to be specific and write down every query param I expect to pass, like: { query: { id: post.id } }. Commented Mar 11, 2024 at 18:51
  • I'm trying to console.log the router to see if the query is being passed through when on the /post page but I can't seem to find the query that I'm passing through. I'm just expecting to see the query itself Commented Mar 12, 2024 at 13:22
  • because you pass too nested object. Have you tried pass query params as I suggested in the previous comment? Commented Mar 12, 2024 at 18:02

2 Answers 2

2

In Next.js, when you wanna pass a query parameter through a Link component, the query should be an object where the keys are the query parameter names and the values are the corresponding values you wanna pass. In your code, it seems like you're trying to pass the entire post object as a query parameter, which may be causing an issue if post contains non-serializable values.

You can try with following updated code for your Link component:

<Link href={{ pathname: "/post/edit", query: { id: post.id } }}>
  <button className="text-teal-600 flex items-center justify-center gap-2 py-2 text-sm">
    <AiFillEdit className="text-2xl" /> Edit
  </button>
</Link>
Sign up to request clarification or add additional context in comments.

1 Comment

I get the same result even when trying your solution. No query shows up. I do however want to pass the entire post object
0

You probably need to be more specific with the data you're passing, try like this:

<Link
  href={{
    pathname: '/test',
    query: { name: 'test' },
  }}
>
  Test
</Link>

1 Comment

I've tried that and it is the same result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.