Skip to content

Instantly share code, notes, and snippets.

@dvtng
Created December 18, 2019 22:00
Show Gist options
  • Save dvtng/ce1f55df08e6a974ab5d8618bbb202a0 to your computer and use it in GitHub Desktop.
Save dvtng/ce1f55df08e6a974ab5d8618bbb202a0 to your computer and use it in GitHub Desktop.
// router concepts
// - route declaration (abstract) vs. route implementation (concrete)
// - transitionIds
// - onPrepare, onTransitionStart, onTransitionEnd
// - global click handler
// - useRouterState, useRouterActions
type RouteInfo = {
name: string;
params: { [key]: string };
query: { [key]: string };
hash: string;
}
type RouterState = {
history: RouteInfo[];
current: RouteInfo;
}
const VIEW_PAGE = new Route<{ spaceKey: string, contentId: string, slug?: string }>({
// abstract
name: "VIEW_PAGE",
pattern: "/wiki/spaces/:spaceKey/pages/:contentId/:slug?",
condition: (state) => true,
// concrete
render: (state) => {},
onPrepare: (state) => {},
onTransitionStart: (state) => {},
onTransitionEnd: (state) => {}
});
const MyComponent = () => {
const { push } = useRouterActions();
return (
<button onClick={() => push(VIEW_PAGE.toUrl({ spaceKey: "CONF", contentId: "123456789" })}>
Go to page
</button>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment