|
| 1 | +import { Callout } from '#components/callout/callout.tsx'; |
| 2 | + |
| 3 | +export const meta = { |
| 4 | + category: 'Router', |
| 5 | + title: 'BrowserRouter', |
| 6 | +}; |
| 7 | + |
| 8 | +# Router |
| 9 | + |
| 10 | +Package provides `BrowserRouter` - basic implementation for controlling _client routing_ in browser. |
| 11 | + |
| 12 | +<Callout> |
| 13 | + <Callout.Heading>SSR ready</Callout.Heading> |
| 14 | + <Callout.Main> |
| 15 | + Can be used in Node.js for Server Side Rendering or Static Site Generation, see next articles. |
| 16 | + </Callout.Main> |
| 17 | +</Callout> |
| 18 | + |
| 19 | +### Basic usage |
| 20 | + |
| 21 | +```tsx |
| 22 | +import { BrowserRouter } from '@krutoo/utils/router'; |
| 23 | + |
| 24 | +const router = new BrowserRouter(); |
| 25 | + |
| 26 | +// connect router to Web APIs |
| 27 | +router.connect(); |
| 28 | + |
| 29 | +// now we can use it to get location info... |
| 30 | +console.log(router.getLocation().pathname); |
| 31 | + |
| 32 | +// ...and for redirects |
| 33 | +router.navigate('/profile/settings'); |
| 34 | + |
| 35 | +// external links also supported |
| 36 | +router.navigate('https://google.com'); |
| 37 | + |
| 38 | +// you can navigate by history (back for example) |
| 39 | +router.go(-1); |
| 40 | +``` |
| 41 | + |
| 42 | +### React bindings |
| 43 | + |
| 44 | +Package provides some hooks and context for working with router in components. |
| 45 | + |
| 46 | +You need to wrap your root component to special `RouterContext` to make router specific hooks working: |
| 47 | + |
| 48 | +```tsx |
| 49 | +import { createRoot } from 'react-dom'; |
| 50 | +import { BrowserRouter } from '@krutoo/utils/router'; |
| 51 | +import { RouterContext } from '@krutoo/utils/react'; |
| 52 | +import { App } from '#components/app'; |
| 53 | + |
| 54 | +// we use provided implementation here but you can use your own |
| 55 | +const router = new BrowserRouter(); |
| 56 | + |
| 57 | +// to make it works you need to call connect |
| 58 | +router.connect(); |
| 59 | + |
| 60 | +createRoot(document.querySelector('#root')).render( |
| 61 | + <RouterContext value={router}> |
| 62 | + <App /> |
| 63 | + </RouterContext>, |
| 64 | +); |
| 65 | +``` |
| 66 | + |
| 67 | +Now you can implement simple routing for example like this: |
| 68 | + |
| 69 | +```tsx |
| 70 | +const ROUTES = [ |
| 71 | + { |
| 72 | + path: '/', |
| 73 | + render: () => <MainPage />, |
| 74 | + }, |
| 75 | + { |
| 76 | + path: '/profile', |
| 77 | + render: () => <ProfilePage />, |
| 78 | + }, |
| 79 | + { |
| 80 | + path: '/items/:itemId', |
| 81 | + render: () => <ItemPage />, |
| 82 | + }, |
| 83 | +]; |
| 84 | + |
| 85 | +function App() { |
| 86 | + const { pathname } = useLocation(); |
| 87 | + |
| 88 | + // find current route |
| 89 | + const currentRoute = useMemo(() => { |
| 90 | + for (const route of ROUTES) { |
| 91 | + const pattern = new URLPattern({ pathname: route.path }); |
| 92 | + |
| 93 | + if (pattern.test({ pathname })) { |
| 94 | + return route; |
| 95 | + } |
| 96 | + } |
| 97 | + }, [pathname]); |
| 98 | + |
| 99 | + // render current route |
| 100 | + <>{currentRoute?.render()}</>; |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +And of course you can use other hooks in any of your components: |
| 105 | + |
| 106 | +```tsx |
| 107 | +function ItemPage() { |
| 108 | + // current location info |
| 109 | + const { pathname, hash, search } = useLocation(); |
| 110 | + |
| 111 | + // route params |
| 112 | + const { groupId, itemId } = useRouteParams('/items/:groupId/:itemId'); |
| 113 | + |
| 114 | + // navigate function |
| 115 | + const navigate = useNavigate(); |
| 116 | + |
| 117 | + return <>{/* ... */}</>; |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### Using for SSR or SSG |
| 122 | + |
| 123 | +`BrowserRouter` can be used in Node.js (or other server environment) to implement Server Side Rendering or Static Site Generation. |
| 124 | + |
| 125 | +You just don't call `connect()` method because under the hood it accesses browser APIs. |
| 126 | + |
| 127 | +Next example shows how you can implement SSR with React: |
| 128 | + |
| 129 | +```tsx |
| 130 | +import express from 'express'; |
| 131 | +import { renderToString } from 'react-dom'; |
| 132 | +import { BrowserRouter } from '@krutoo/utils/router'; |
| 133 | +import { RouterContext } from '@krutoo/utils/react'; |
| 134 | +import { ROUTES } from '#app/routes'; |
| 135 | +import { App } from '#components/app'; |
| 136 | + |
| 137 | +const app = express(); |
| 138 | + |
| 139 | +for (const route of ROUTES) { |
| 140 | + app.get(ROUTES.path, (req, res) => { |
| 141 | + const router = new BrowserRouter({ |
| 142 | + defaultLocation: { pathname: req.path }, |
| 143 | + }); |
| 144 | + |
| 145 | + const markup = renderToString( |
| 146 | + <RouterContext value={router}> |
| 147 | + <App /> |
| 148 | + </RouterContext>, |
| 149 | + ); |
| 150 | + |
| 151 | + res.send(markup); |
| 152 | + }); |
| 153 | +} |
| 154 | + |
| 155 | +app.listen(8080, () => { |
| 156 | + console.log(`Server running at http://localhost:${8080}`); |
| 157 | +}); |
| 158 | +``` |
0 commit comments