How to Share Data Between Pages in Next.js?
Last Updated :
24 Jul, 2024
Sharing data between pages in Next.js can be crucial for maintaining state and passing information efficiently. You can achieve this with the help of URL parameters. In this article, we will learn about how to share data between pages in next.js
Approach to Share Data Between Pages
- Use the Link component from Next.js and pass an href prop containing an object with pathname and query properties.
- Set the pathname to the target page's route and the query to an object with the data you want to share.
- In the target page component, use the useRouter hook from 'next/router' to access the router object.
- Extract the data from the query object within the target page component.
- Test the navigation and data access to ensure the query parameters are correctly passed and retrieved.
Steps to Setup a NextJS App
Step 1: Create a NextJS application using the following command and answer a few questions.
npx create-next-app@latest app_name
Step 2: It will ask you some questions, so choose as the following.
√ Would you like to use TypeScript? ... No
√ Would you like to use ESLint? ... Yes
√ Would you like to use Tailwind CSS? ... No
√ Would you like to use `src/` directory? ... Yes
√ Would you like to use App Router? ... Yes
√ Would you like to customize the default import alias (@/*)? ... No
Step 3: After creating your project folder, move to it using the following command.
cd app_name
Project Structure:
The updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "14.2.3"
},
"devDependencies": {
"postcss": "^8",
"tailwindcss": "^3.4.1"
}
Example: This example demonstrates how to pass the data between pages in next.js with the help of URL parameters
JavaScript
//File path: src/app/page.js
import Link from "next/link";
export default function Home() {
return (
<>
<h3>
Sharing data between
pages | GeeksForGeeks
</h3>
<Link href={{
pathname: '/about',
query: { course: ["JavaScript", "Java", "Data Strucutre"] },
}}>
About Page
</Link>
</>
);
}
JavaScript
//File path: src/app/about/page.js
export default function Page({ searchParams }) {
const courseList = searchParams.course
return (
<>
<h1>Course List</h1>
<ul>
{
courseList.map((course) => {
return
<li key={course}>
{course}
</li>
})
}
</ul>
</>
)
}
Step to Run Application: Run the application using the following command from the root directory of the project
npm run dev
Output: Your project will be shown in the URL http://localhost:3000/
Similar Reads
How to Get URL pathname in Next.js? Next.js is a powerful React framework that simplifies the process of building server-rendered and statically generated web applications. One common requirement in web development is to get the URL pathname, which can be useful for various purposes such as conditional rendering, routing, and analytic
3 min read
How to add Web Share in Next.js ? The Web Share API enables web applications to share content (like URLs, text, or files) to other apps installed on a user's device, such as social media platforms, messaging apps, or email clients. Integrating the Web Share API into a Next.js project enhances user experience by providing a seamless
2 min read
How To Handle Loading Between Page Changes in NextJS? To experience smooth transitions between the pages of your Next.js application, you can include a loading indicator that shows the progress of the page being loaded. This can be done by creating a custom spinner component and using it during page transitions. In this article, we will create a custom
4 min read
Linking between pages in Next.js In this article, we are going to see how we can link one page to another in Next.js. Follow the below steps to set up the linking between pages in the Next.js application:To create a new NextJs App run the below command in your terminal:npx create-next-app GFGAfter creating your project folder (i.e.
2 min read
How To Get Current Route In Next.js? Next.js is a popular React framework that makes it easy to build server-side rendered and static web applications. One common task in web development is determining the current route or URL of the page. In Next.js, this can be done using the built-in useRouter hook. This article will guide you throu
3 min read