how do i access the id query from url
Answered
Otterhound posted this in #help-forum
OtterhoundOP
so i redirect to the id here
<Link href={`/addOrder?id=${product._id}`}>
<button className='btn btn-primary'>Buy Now</button>
</Link> and now while accessing it "use client"
import { useRouter } from "next/navigation";
import { useState } from "react";
export default function OrderForm() {
const location = useRouter();
const productId = location.query.id;
console.log(productId);
const [orderData, setOrderData] = useState({
userId: "6548534f3c3c275b576a6c42",
productId: productId || "",
quantity: 1,
}); i did the try accessing it but it it is giving me undefinedAnswered by American Chinchilla
4 Replies
American Chinchilla
Answer
American Chinchilla
so for you that'd look like:
const searchParams = useSearchParams();
const productId = searchParams.get('id')American Chinchilla
yep!