@maxima/a-router
v0.0.16
Published
Simple frontend routing library for React using TypeScript & React hooks.
Downloads
75
Readme
ARouter
Simple frontend routing library for React using TypeScript & React hooks.
How to install
yarn install a-router
How to use
const Sample: React.SFC<{}> = (props) => {
const routes = [
{ path: '/', component: RootComponent },
{ path: '/entries', component: EntriesComponent },
{ path: '/entries/:id(.:format)', component: EntryDetailsComponent, constraints: { id: /\d+/, format: /html|json/ } }
];
return useRoutes(routes);
};
API
RouterLocation
pathname: string
- location pathnamesearch: string
- search part of the location urlhash: string
- hash part of the location urlqueryParams: { [key: string]: any }
- object representing extracted query parameters
Route
path: string
- route path which will be matched against current locationconstraints?: { [key: string]: RegExp }
- route constraints (object with parameter names and regular expressions as constraints)exact?: boolean
- flag which indicates whether route path should match current location exactly or notcomponent: React.ComponentType<{ match: T | undefined; location: RouterLocation; }>
- component to be rendered for this route
useRoutes hook params
routes: Array<Route<any>>
- array of routes to match against router location
usePrompt hook params
visible: boolean
- flag which indicates whether navigation to other paths/locations has to be blocked or notmessage: string
- message to show when the navigation is blocked and user still tried to navigate to other path/location
useRedirect hook params
from: string
- redirect source pathto: string
- redirect destination path
List of props available in route components
match: T | undefined
- list of query parameters matched for the routelocation: RouterLocation
- current router location
Licence
GPLv3
Copyright hck, 2019