Skip to content

Commit 0438a5c

Browse files
albertogasparinMonicaOlejniczak
authored andcommitted
Consolidate Router component
1 parent 1240881 commit 0438a5c

File tree

41 files changed

+503
-1320
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+503
-1320
lines changed

docs/_sidebar.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
* **Router**
1212

1313
- [Configuration](/router/configuration.md)
14-
- [Selection](/router/selection.md)
1514
- [State](/router/state.md)
1615
- [SSR](/router/ssr.md)
1716

docs/api/components.md

Lines changed: 13 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -29,82 +29,34 @@ import { appRoutes } from './routing';
2929

3030
### Router props
3131

32-
| prop | type | description |
33-
| ----------------- | ------------------------- | -------------------------------------------------------------------------------------------------- |
34-
| `routes` | `Routes[]` | Your application's routes |
35-
| `initialRoute` | `Route` | The route your application is initially showing |
36-
| `history` | `History` | The history instance for the router |
37-
| `basePath` | `string` | Base path string that will get prepended to all route paths |
38-
| `resourceContext` | `ResourceContext` | Custom contextual data that will be provided to all your resources' `getKey` and `getData` methods |
39-
| `resourceData` | `ResourceData` | Pre-resolved resource data. When provided, the router will not request resources on mount |
40-
| `onPrefetch` | `function(RouterContext)` | Called when prefetch is triggered from a Link |
41-
42-
## StaticRouter
43-
44-
If you are planning to render your application on the server, you must use the `StaticRouter` in your server side entry. The `StaticRouter` should only be used on server as it omits all browser-only resources. It does not require a `history` prop to be provided, instead, you simply need to provide the current `location` as a string. In order to achieve this, we recommend your server side application uses [`jsdom`](https://github.com/jsdom/jsdom).
45-
46-
```js
47-
// server-app.js
48-
import { StaticRouter } from 'react-resource-router';
49-
import { App } from '../components';
50-
import { appRoutes } from '../routing';
51-
52-
const { pathname, search } = window.location;
53-
const location = `${pathname}${search}`;
54-
55-
export const ServerApp = () => (
56-
<StaticRouter routes={appRoutes} location={location}>
57-
<App />
58-
</StaticRouter>
59-
);
60-
```
61-
62-
### StaticRouter props
63-
64-
| prop | type | description |
65-
| ---------- | ---------- | ----------------------------------------------------------- |
66-
| `routes` | `Routes[]` | Your application's routes |
67-
| `location` | `string` | The string representation of the app's current location |
68-
| `basePath` | `string` | Base path string that will get prepended to all route paths |
32+
| prop | type | description |
33+
| ----------------- | ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
34+
| `routes` | `Routes[]` | Your application's routes |
35+
| `history` | `History` | The history instance for the router, if omitted memory history will be used (optional but recommended) |
36+
| `location` | `string` | If `history` prop is omitted, this configures the initial location for the default memory history (optional, useful for tests and storybooks) |
37+
| `basePath` | `string` | Base path string that will get prepended to all route paths (optional) |
38+
| `initialRoute` | `Route` | The route your application is initially showing, it's a performance optimisation to avoid route matching cost on initial render(optional) |
39+
| `resourceContext` | `ResourceContext` | Custom contextual data that will be provided to all your resources' `getKey` and `getData` methods (optional) |
40+
| `resourceData` | `ResourceData` | Pre-resolved resource data. When provided, the router will not request resources on mount (optional) |
41+
| `onPrefetch` | `function(RouterContext)` | Called when prefetch is triggered from a Link (optional) |
6942

7043
## MemoryRouter
7144

7245
The `MemoryRouter` component can be used for your application's unit tests.
7346

7447
```js
7548
it('should send right props after render with routes', () => {
76-
mount(
77-
<MemoryRouter routes={[mockRoutes[0]]}>
78-
<RouterSubscriber>
79-
{({ history, location, routes, route, match, query }) => {
80-
expect(history).toEqual(mockHistory);
81-
expect(location).toEqual(mockLocation);
82-
expect(routes).toEqual(routes);
83-
expect(route).toEqual(
84-
expect.objectContaining({
85-
path: `/pathname`,
86-
})
87-
);
88-
expect(match).toBeTruthy();
89-
expect(query).toEqual({
90-
foo: 'bar',
91-
});
92-
93-
return <div>I am a subscriber</div>;
94-
}}
95-
</RouterSubscriber>
96-
</MemoryRouter>
97-
);
49+
render(<MemoryRouter routes={[mockRoutes[0]]}>{/* ... */}</MemoryRouter>);
9850
});
9951
```
10052

10153
### MemoryRouter props
10254

10355
| prop | type | description |
10456
| ---------- | ---------- | ----------------------------------------------------------- |
105-
| `routes` | `Routes[]` | Your application's routes |
106-
| `location` | `string` | The string representation of the app's current location |
10757
| `basePath` | `string` | Base path string that will get prepended to all route paths |
58+
| `location` | `string` | The string representation of the app's current location |
59+
| `routes` | `Routes[]` | Your application's routes |
10860

10961
## Link component
11062

docs/router/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
- **Router**
22

33
- [Configuration](./configuration.md)
4-
- [Selection](./selection.md)
54
- [State](./state.md)
65
- [SSR](./ssr.md)

docs/router/selection.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/router/ssr.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,25 @@ import { RouteComponent } from 'react-resource-router';
2222

2323
export const App = () => (
2424
<>
25+
<Navigation />
2526
<RouteComponent />
27+
<Footer />
2628
</>
2729
);
2830
```
2931

30-
The reason for this is that currently, you will need to use the [`Router`](#router-component) component on the client and the [`StaticRouter`](#staticrouter-component) component on the server. Following the above composition pattern will allow you to use the correct router in your server side entry and client side entry respectively. This could look something like the following examples:
32+
When however you need to SSR your app, we need pass different props to Router, as `createBrowserHistory` does not really work on server, so we recommend to use `location` instead (or pass your own `MemoryHistory` if needed)
3133

3234
```js
3335
// server-app.js
34-
import { StaticRouter } from 'react-resource-router';
36+
import { Router } from 'react-resource-router';
3537
import { App } from '../components';
38+
import { routes } from '../routing/routes';
3639

37-
export const ServerApp = ({ location, routes }) => (
38-
<StaticRouter routes={routes} location={location}>
40+
export const ServerApp = ({ location }) => (
41+
<Router routes={routes} location={location}>
3942
<App />
40-
</StaticRouter>
43+
</Router>
4144
);
4245
```
4346

@@ -60,21 +63,21 @@ export const ClientApp = () => (
6063

6164
Until React Suspense works on the server, we cannot do progressive rendering server side. To get around this, we need to `await` all resource requests to render our app _with all our resource data_ on the server.
6265

63-
Luckily the `StaticRouter` provides a convenient static method to do this for us.
66+
Luckily the `Router` provides a convenient static method to do this for us.
6467

6568
```js
6669
import { renderToString } from 'react-dom/server';
67-
import { StaticRouter } from 'react-resource-router';
70+
import { Router } from 'react-resource-router';
6871
import { routes } from '../routing/routes';
6972
import { ServerApp } from './app';
7073

7174
const renderToStringWithData = async ({ location }) => {
72-
await StaticRouter.requestResources({ location, routes });
75+
await Router.requestResources({ location, routes });
7376

74-
return renderToString(<ServerApp routes={routes} location={location} />);
77+
return renderToString(<ServerApp location={location} />);
7578
};
7679
```
7780

78-
Notice that we do not need to provide any `resourceData` object to the `ServerApp`, the `StaticRouter` handles this for us internally.
81+
Notice that we do not need to provide any `resourceData` object to the `ServerApp`, the `Router` handles this for us internally.
7982

80-
To prevent slow APIs from causing long renders on the server you can optionally pass in `timeout` as an option to `StaticRouter.requestResources`. If a route resource does not return within the specified time then its data and promise will be set to null.
83+
To prevent slow APIs from causing long renders on the server you can optionally pass in `timeout` as an option to `Router.requestResources`. If a route resource does not return within the specified time then its data and promise will be set to null.

examples/hash-routing/about.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
3+
import { Link } from 'react-resource-router';
4+
5+
export const About = () => (
6+
<div>
7+
<h1>About</h1>
8+
<Link to="/">Go to home</Link>
9+
</div>
10+
);

examples/hash-routing/home.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import React from 'react';
2+
3+
import { Link } from 'react-resource-router';
4+
5+
export const Home = () => (
6+
<div>
7+
<h1>Home</h1>
8+
<Link to="/about">Go to about</Link>
9+
</div>
10+
);

examples/hash-routing/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Example - Hash Routing</title>
5+
</head>
6+
7+
<body>
8+
<div id="root"></div>
9+
<script src="bundle.js" type="text/javascript"></script>
10+
</body>
11+
</html>

examples/hash-routing/index.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { createHashHistory as createHashHistory4 } from 'history';
2+
import { createHashHistory as createHashHistory5 } from 'history-5';
3+
import React from 'react';
4+
import { render } from 'react-dom';
5+
6+
import { About } from './about';
7+
import { Home } from './home';
8+
9+
import { Redirect, RouteComponent, Router } from 'react-resource-router';
10+
11+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
12+
const history4 = createHashHistory4();
13+
const history5 = createHashHistory5();
14+
const history = history5;
15+
16+
const routes = [
17+
{
18+
name: 'home',
19+
path: '/home',
20+
exact: true,
21+
component: () => <Home />,
22+
},
23+
{
24+
name: 'about',
25+
path: '/about',
26+
exact: true,
27+
component: () => <About />,
28+
},
29+
{
30+
name: 'default',
31+
path: '/*',
32+
exact: true,
33+
component: () => <Redirect to="/home" />,
34+
},
35+
];
36+
37+
const App = () => {
38+
return (
39+
<Router history={history} routes={routes}>
40+
<RouteComponent />
41+
</Router>
42+
);
43+
};
44+
45+
render(<App />, document.getElementById('root'));

examples/hooks/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { render } from 'react-dom';
33

44
import Home from './home';
55
import PathParamExample from './use-path-param';
6-
import QueryParamExample from './use-query-param';
76

87
import {
98
Router,
@@ -12,6 +11,8 @@ import {
1211
createRouterSelector,
1312
} from 'react-resource-router';
1413

14+
import QueryParamExample from './use-query-param';
15+
1516
const myHistory = createBrowserHistory();
1617
const useRouteName = createRouterSelector(s => s.route.name);
1718

0 commit comments

Comments
 (0)