Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next Js Router.push is not a function error

When I try to redirect using Router.push() I get the following error:

TypeError: next_router__WEBPACK_IMPORTED_MODULE_3__.Router.push is not a function

I am trying to migrate from create-react-app to next js.

const redirectUser = () => {
        if (true) {
            Router.push('/');
        }
    };
like image 387
Ryan Dhungel Avatar asked Nov 27 '22 02:11

Ryan Dhungel


2 Answers

I had to import like so:

// works
import Router from "next/router";
// dont
import { Router } from "next/router";
like image 182
Ryan Dhungel Avatar answered Dec 04 '22 07:12

Ryan Dhungel


Don't add curly bracket while importing Router from next/router

Use this:

import Router from "next/router";
like image 37
Chanchala Gorale Avatar answered Dec 04 '22 06:12

Chanchala Gorale