Replies: 3 comments 2 replies
-
I wound up with something like this: import {
Accessor,
createContext,
createSignal,
For,
onCleanup,
onMount,
ParentProps,
Show,
useContext,
} from 'solid-js';
import { A } from 'solid-start';
type Crumb = {
title: string;
href: string;
};
const BreadcrumbContext =
createContext<
readonly [
Accessor<Array<Crumb>>,
{ addCrumb: (crumb: Crumb) => void; removeCrumb: (crumb: Crumb) => void },
]
>();
export function BreadcrumbProvider(props: ParentProps) {
const [crumbs, setCrumbs] = createSignal<Array<Crumb>>([]);
const value = [
crumbs,
{
addCrumb(crumb: Crumb) {
setCrumbs((a) => [...a, crumb]);
},
removeCrumb({ title, href }: Crumb) {
setCrumbs((a) => a.filter((c) => c.title !== title && c.href !== href));
},
},
] as const;
return (
<BreadcrumbContext.Provider value={value}>
{props.children}
</BreadcrumbContext.Provider>
);
}
export function useCrumb(crumb: Crumb) {
const [, { addCrumb, removeCrumb }] = useContext(BreadcrumbContext)!;
onMount(() => addCrumb(crumb));
onCleanup(() => removeCrumb(crumb));
}
export default function PageBreadcrumbs() {
const [crumbs] = useContext(BreadcrumbContext)!;
const lastCrumb = () => {
const list = crumbs();
return list[Math.max(list.length - 1, 0)];
};
const initialCrumbs = () => {
const list = crumbs();
return list.slice(0, -1);
};
return (
<Show when={crumbs().length > 1}>
{/* render breadcrumbs */}
</Show>
);
} In my route components I just call
Does anyone have any ideas on how to update this to be SSR-friendly? |
Beta Was this translation helpful? Give feedback.
-
If you are on the server, instead of using lifecycle events, store the current crumbs in the request event immediately, and forgo the cleanup. |
Beta Was this translation helpful? Give feedback.
-
Recently I used Here's a hint in the docs: https://docs.solidjs.com/solid-router/reference/primitives/use-current-matches Simple Example<Route path="/" component={() => <></>} info={{breadcrumb: "content"}} />
// ...
const matches = useCurrentMatches()
console.log(matches()) // Find info yourself in the console More Realistic ExampleYou can inject an <Route
path="/"
component={() => {
const params = useParams()
return <h1>Hello, {params.name}!</h1>
}}
info={{crumb: (params) => <>{params.name}</>}} /> Then you can use it anywhere under const params = useParams()
const matches = useCurrentMatches()
const crumbs = createMemo(() =>
matches().map((match) =>
match.info.crumb({name: params.name})))
return <ul>
<For each={crumbs()}>{(crumb) =>
<li>/ {crumb}</li>
}</For>
</ul> |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Does anyone have an example of how one might produce automatically-generated breadcrumbs in Solid Start? In Remix I would use
export const meta
to export some metainformation (like a breadcrumb title) and then in a top-level layout I could take all of that along with the associated href values and assemble breadcrumbs.So far I'm thinking I would have to store some sort of map for this meta information in a layout breadcrumbs component and have it use
useLocation()
to determine what to render.Beta Was this translation helpful? Give feedback.
All reactions