admin管理员组

文章数量:1288010

I need to deduct the hash change with URL. for that I tried all this way from here:

How to detect change in the URL hash in Next.js?

Nothing works, But if I set Timeout it's working.

useEffect(() => {
  setTimeout(() => {
      console.log(window && window.location.hash)
  },1000)
});

Need to know what's happening or what would be the correct way to handle? though it is working, need to know the background things what is causing.

it's not working

const params = useParams();
const [url, setURL] = useState<string>('')
useEffect(() => {
    const update = () => {
        // set url from path in forward.
        setURL(window.location.href.substring(window.location.href.indexOf('/', 9)))
    };
    window.addEventListener('hashchange', update);
    update(); // Initial fetch
    return () => window.removeEventListener('hashchange', update)
}, [params])
console.log(url)

本文标签: nextjs15Nextjs URL hash change only works with quotsetTimeoutquotStack Overflow