#get final url from client side component
1 messages · Page 1 of 1 (latest)
Hi @shell herald. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Next.js, you can access the URL from a client-side component using the window object. However, you need to ensure that you're accessing it only when the component is running in the browser because the window object is not available on the server. Here's an example:
'use client';
import { useEffect } from 'react';
export default function ClientComponent() {
useEffect(() => {
// You now have access to `window`
console.log(window.location.href);
}, [])
return ...;
}
This code will log the current URL to the console when the component mounts. Remember, this code will only work on the client-side because the window object is not available during server-side rendering.