简体   繁体   中英

Router.push redirecting but not rendering the page

Hi I am trying to implement simple route in next js. Here is my code

import React, { useEffect, useState } from "react";
import Router from "next/router";

import NavBar from "../components/NavBar";


function Index() {

  return (
    <div>
      <h1>Hi Guys!! Welcome!!!</h1>
      <h3>Select your role:</h3>
      <div onClick={() => Router.replace("/admin")}>
        Admin
      </div>
    </div>
  );
}

export default Index;

Now when i click the admin page, it redirects it to admin page but does not renders the component. i have to press enter on the component to render it

here is my admin component:

import React from 'react';

function Admin(){


    return <h1>Welcome to admin Dashboard</h1>
}

export default Admin;

not sure but you can try to use useRouter instead

import { useRouter } from 'next/router';

const router = useRouter();
onClick={() => router.push("/admin");

尝试

Router.replace("path to js file", "/admin")

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