diff --git a/src/components/App.tsx b/src/components/App.tsx index e45452df..523e052d 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -4,6 +4,7 @@ import { GrowthBook, GrowthBookProvider } from '@growthbook/growthbook-react'; import Header from '@components/Header'; import Alert from '@components/Alert'; +import Breadcrumbs from '@components/Breadcrumbs'; // Create a GrowthBook instance const growthbook = new GrowthBook({ @@ -23,8 +24,7 @@ function App() { // Load feature definitions from API fetch( - `https://cdn.growthbook.io/api/features/${ - import.meta.env.VITE_GROWTH_BOOK_KEY + `https://cdn.growthbook.io/api/features/${import.meta.env.VITE_GROWTH_BOOK_KEY }`, ) .then((res) => res.json()) @@ -48,6 +48,7 @@ function App() { return (
+ diff --git a/src/components/Breadcrumbs.css b/src/components/Breadcrumbs.css new file mode 100644 index 00000000..467447a4 --- /dev/null +++ b/src/components/Breadcrumbs.css @@ -0,0 +1,5 @@ +.breadcrumbs { + display: flex; + flex-direction: row; + gap: 5px; +} \ No newline at end of file diff --git a/src/components/Breadcrumbs.tsx b/src/components/Breadcrumbs.tsx new file mode 100644 index 00000000..97f1be62 --- /dev/null +++ b/src/components/Breadcrumbs.tsx @@ -0,0 +1,22 @@ +import { useMatches } from "react-router-dom"; +import './Breadcrumbs.css'; + +export function Breadcrumbs() { + let matches = useMatches(); + let crumbs = matches + // first get rid of any matches that don't have handle and crumb + .filter((match) => Boolean(match.handle?.crumb)) + // now map them into an array of elements, passing the loader + // data to each one + .map((match) => match.handle.crumb(match.data)); + + return ( +
    + {crumbs.map((crumb, index) => ( +
  1. {crumb}
  2. + ))} +
+ ); +} + +export default Breadcrumbs; diff --git a/src/index.jsx b/src/index.jsx index fb751935..2ed33388 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -1,6 +1,6 @@ import { lazy, StrictMode, Suspense } from 'react'; import { createRoot } from 'react-dom/client'; -import { createBrowserRouter, RouterProvider } from 'react-router-dom'; +import { createBrowserRouter, RouterProvider, Link } from 'react-router-dom'; import './index.css'; @@ -20,6 +20,12 @@ const router = createBrowserRouter( { path: '/', element: , + loader: ({ params }) => { + return params; + }, + handle: { + crumb: () => Home, + }, children: [ { path: '/', @@ -44,10 +50,22 @@ const router = createBrowserRouter( }, { path: 'users', + loader: ({ params }) => { + return params; + }, + handle: { + crumb: () =>

Users

, + }, children: [ { path: ':userName', element: , + loader: ({ params }) => { + return params; + }, + handle: { + crumb: (data) => {data.userName}, + }, }, ], },