next js page loader
- Create a Next.js Project:
Use
npx create-next-app
to generate a new Next.js project.Navigate to the Pages Directory:
Move to the
pages
directory in your Next.js project.Create a New Page:
Create a new JavaScript file for your page, for example,
example.js
.Import the useRouter Hook:
Import the
useRouter
hook fromnext/router
.javascript import { useRouter } from 'next/router';
Define the Page Component:
Define a React component for your page.
javascript function Example() { // Your component code here }
Use the useRouter Hook:
Use the
useRouter
hook to get therouter
object.javascript const router = useRouter();
Access Route Parameters:
Use the
router.query
object to access route parameters.javascript const { param1, param2 } = router.query;
Render the Component:
Render your component and display the route parameters.
javascript return ( <div> <p>Parameter 1: {param1}</p> <p>Parameter 2: {param2}</p> </div> );
Export the Component:
Export your component as the default export.
javascript export default Example;
Start the Development Server:
- Run
npm run dev
to start the Next.js development server.
- Run
Visit the Page:
- Open your browser and go to
http://localhost:3000/example/parameter1/parameter2
to see your page in action.
- Open your browser and go to