简体   繁体   中英

Unable to use router.push in Reactjs

I am working on Reactjs and using nextjs,Right now i am trying to refresh page (using routes) but i am getting following error

Error: No router instance found. you should only use "next/router" inside the client side of your app.

Here is my current code

const Post = ({ post, blogs }) => {
    const routers = useRouter();
    var spath=routers.asPath;
    routers.push({ pathname: spath, query: '' });
})

You probably used router outside of a functional component or hook. Convert Post to hook and it should work.

If you are running this code on application load or on any data change. You should do this inside useEffect hook because while rendering application on sever router won't be there and useEffect runs only inside browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM