2.1.0 • Published 10 months ago

solidjs-sense-router v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

solidjs-sense-router

The SolidJS Router that make sense.

Installation

npm install solidjs-sense-router or yarn add solidjs-sense-router

Why not solid-router

Please take a look at https://github.com/solidjs/solid-router/issues/64 and newBase api of this library

Usage

Sample:

import { Component, lazy } from "solid-js";
import { render } from 'solid-js/web';
import { useRoutes, RouteDefinition } from "solidjs-sense-router";

const routes: RouteDefinition[] = [
  {
    path: "/",
    component: lazy(() => import("./pages/home")),
  },
  {
    path: "/product",
    redirectTo: '/good'
  },
  {
    path: "/good",
    component: lazy(() => import("./pages/good")),
    children: [
      {
        path: "/food",
        component: lazy(() => import("./pages/good/food")),
      },
      {
        path: "/fruit",
        component: lazy(() => import("./pages/good/fruit")),
      },
    ],
    prefetch: true;
    canLoad: () => {
       ...
       return true;
    }
  },
  {
    path: "/news/:id?",
    component: lazy(() => import("./pages/news")),
  },
  {
    path: "/*all",
    component: lazy(() => import("./pages/not-found")),
  },
];

const App: Component = () => {
  const Routes = useRoutes(routes);
  return (
    <Router>
      <Routes />
    </Router>
  );
};

render(() => <App />, document.getElementById('root') as HTMLElement);

API

Components:

  • <Router ...props />

    • props:
      • children: JSX.Element
      • url: current url (must be set in server mode)
      • defaultBase: default base url (default: '')
      • maxKeepAlive: max element for keepAlive, default unlimited
  • <Link ...props />

    • props: same as a tag and more have:
      • replace: if true, replace current url (default: false)
      • state: state object to push in history (default: undefined)
      • queryParams?: Record<string, string> query params
      • activeClass?: class name witch will be added to a tag when link's href is match by current route
      • prefetch?: 'immediate' | 'visible' | 'hover' | 'none'
        • immediate: prefetch when link is rendered
        • visible: prefetch when link is visible
        • hover: prefetch when mouse is over link
        • none: don't prefetch
  • <Outlet /> nest child
  • <KeepAlive ...props /> Keep components alive even after parent's unmounts, saving signals and DOM elements in cache to reuse them. idea from solid-keep-alive
    • props:
      • children: JSX.Element
      • id: unique id for cache
      • onShow call when show
      • onHide call when hide

Hooks:

  • useRoutes(route: RouteDefinition | RouteDefinition[])

    return Routes

  • useLoading(): Accessor<boolean>

    if page is loading

  • useLocation(): { url: Accessor<URL>; fullUrl: Accessor<URL>; base: Accessor<string>; state: Accessor<any>; }

    return url, fullUrl, base, state

  • useNavigator(): { navigate, newBase }

    • navigate(...): navigate to new route
    • newBase(...): change base of url
  • useQueryParams()

    return query params

  • useRouteParams()

    return route params

  • useCurrentMatch(path: string): RouteDefinition | undefined

    return current route match by path

  • useMatch(path: string): RouteDefinition[]

    return route match by path

  • usePrefetch(path: string | string[]): prefetch route match by path

  • useRouteAction(): [Accessor<ActionType>, Accessor<number>] return route action (forward/backward) and current route number which start from 0

    • ActionType: forward | backward current route is forward or backward
  • onRouteLeave((action: ActionType, length: number) => Promise<any>)

    • current route page will unMount until callback's Promise resolve, so you can do some page effects before page leave

Utils:

  • matchRoute(path: string, route: string): { match: boolean; params: Record<string, string> }
    • match: true if path is match by route
    • params: route params match by route
2.0.3

10 months ago

2.0.2

10 months ago

2.0.4

10 months ago

2.1.0

10 months ago

2.0.1

10 months ago

2.0.0

2 years ago

1.9.4

2 years ago

1.9.3

2 years ago

1.9.1

2 years ago

1.6.4

2 years ago

1.9.0

2 years ago

1.8.1

2 years ago

1.6.3

2 years ago

1.8.0

2 years ago

1.6.2

2 years ago

1.7.0

2 years ago

1.6.1

2 years ago

1.9.2

2 years ago

1.6.5

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago