Next Router – Reference

Next JS Routing examples

Router.push

import React from 'react'
import { useRouter } from "next/router";

const Members = () => {
  const router = useRouter();

  if(user?.role !== 'ADMIN'){
    router.push('/dashboard');
  }

  return (
    <div>Members Page</div>
  )
}

export default Members

Query Params

import { useRouter } from "next/router";


export default () => {
  const router = useRouter();
  const { tab } = router.query;


 enum TabEnum {
  Edit = "Edit",
  Share = "Share",
  Responses = "Responses",
  Settings = "Settings",
}

  useEffect(() => {
    if (tab && Object.values(TabEnum).includes(tab as any)) {
      setActiveTab(tab as any);
    }
  }, [tab]);


}

http://localhost:3000/dashboard/surveys/cc238f70-163f-4047-9bd1-312715c4836b?tab=Share

About the Author: smartcoder

You might like

Leave a Reply

Your email address will not be published. Required fields are marked *