How to make BreadCrumbs in Next.js
Published: 2024-01-08 06:38:46

In Next.js, creating breadcrumbs typically involves using a combination of components, context, and routing. Here's a general outline of how you can create breadcrumbs in a Next.js application:

1. **Install Required Packages:**
   Make sure you have the necessary packages installed. You might want to use a state management library like `react-redux` for managing the breadcrumbs state.

   ```bash
   npm install react-redux
   ```

2. **Create a Breadcrumbs Component:**
   Create a reusable Breadcrumbs component that will be responsible for rendering the breadcrumbs.

   ```jsx
   // components/Breadcrumbs.js
   import { useSelector } from 'react-redux';

   const Breadcrumbs = () => {
     const breadcrumbs = useSelector((state) => state.breadcrumbs);

     return (
       <div>
         {breadcrumbs.map((breadcrumb, index) => (
           <span key={index}>
             <Link href={breadcrumb.path}>
               <a>{breadcrumb.label}</a>
             </Link>
             {index < breadcrumbs.length - 1 && <span> / </span>}
           </span>
         ))}
       </div>
     );
   };

   export default Breadcrumbs;
   ```

3. **Create a Breadcrumbs Context:**
   Set up a context to manage the breadcrumbs state.

   ```jsx
   // context/BreadcrumbsContext.js
   import { createContext, useContext, useReducer } from 'react';

   const BreadcrumbsContext = createContext();

   const breadcrumbsReducer = (state, action) => {
     switch (action.type) {
       case 'ADD_BREADCRUMB':
         return [...state, action.payload];
       case 'RESET_BREADCRUMBS':
         return [];
       default:
         return state;
     }
   };

   const BreadcrumbsProvider = ({ children }) => {
     const [state, dispatch] = useReducer(breadcrumbsReducer, []);

     const addBreadcrumb = (breadcrumb) => {
       dispatch({ type: 'ADD_BREADCRUMB', payload: breadcrumb });
     };

     const resetBreadcrumbs = () => {
       dispatch({ type: 'RESET_BREADCRUMBS' });
     };

     return (
       <BreadcrumbsContext.Provider value={{ breadcrumbs: state, addBreadcrumb, resetBreadcrumbs }}>
         {children}
       </BreadcrumbsContext.Provider>
     );
   };

   const useBreadcrumbs = () => {
     return useContext(BreadcrumbsContext);
   };

   export { BreadcrumbsProvider, useBreadcrumbs };
   ```

4. **Integrate Breadcrumbs in Pages:**
   In your pages, use the `BreadcrumbsProvider` and update the breadcrumbs as the user navigates through the application.

   ```jsx
   // pages/[slug].js (or any other page)
   import { BreadcrumbsProvider, useBreadcrumbs } from '../context/BreadcrumbsContext';

   const Page = ({ data }) => {
     const { addBreadcrumb } = useBreadcrumbs();

     useEffect(() => {
       // Add breadcrumb when the page is mounted
       addBreadcrumb({ label: 'Home', path: '/' });
       addBreadcrumb({ label: data.title, path: `/page/${data.slug}` });
     }, [addBreadcrumb, data]);

     return (
       <div>
         {/* Page content */}
       </div>
     );
   };

   export default Page;

   // _app.js (or another top-level component)
   import { BreadcrumbsProvider } from '../context/BreadcrumbsContext';
   import Breadcrumbs from '../components/Breadcrumbs';

   function MyApp({ Component, pageProps }) {
     return (
       <BreadcrumbsProvider>
         <Component {...pageProps} />
         <Breadcrumbs />
       </BreadcrumbsProvider>
     );
   }

   export default MyApp;
   ```

Adjust the code according to your project structure and requirements. This example assumes you're using React and Redux for state management, but you can adapt it to other state management solutions or use local component state if your application is relatively simple.