Skip to content

Commit 1f41763

Browse files
committed
Allow passing handler via "handler" prop
1 parent a38899f commit 1f41763

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ descriptors — `Location` and `NotFound`.
4242
That way `App` will render a markup for a currently active location (via
4343
`window.location.pathname`).
4444

45+
If you don't want to specify handlers for each location inline you can pass them
46+
a `handler` prop with a component class instead:
47+
48+
<Locations>
49+
<Location path="/" handler={MainPage} />
50+
<Location path="/users/:username" handler={UserPage} />
51+
</Locations>
52+
4553
It automatically handles `popstate` event and updates its state accordingly.
4654

4755
To navigate to a different location, component exposes `navigate(path)` method.
@@ -61,7 +69,7 @@ You would want to keep a `ref` to the component to call it:
6169
</Locations>
6270
)
6371
}
64-
72+
6573
That way all clicks to anchors will be intercepted and, instead of reloading the
6674
page, routed via `Locations` component.
6775

index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function createRouter(component) {
4242

4343
if (process.env.NODE_ENV !== "production") {
4444
invariant(
45-
current.children !== undefined && current.path !== undefined,
45+
current.handler !== undefined && current.path !== undefined,
4646
"Router should contain either Route or NotFound components as children")
4747
}
4848

@@ -60,27 +60,27 @@ function createRouter(component) {
6060
}
6161
}
6262

63-
var children = page ? page.children :
64-
notFound ? notFound.children :
63+
var handler = page ? page.handler :
64+
notFound ? notFound.handler :
6565
[];
6666

67-
return this.transferPropsTo(component(null, children(match)));
67+
return this.transferPropsTo(component(null, handler(match)));
6868
}
6969
});
7070
}
7171

72-
function Route(props, children) {
72+
function Route(props, handler) {
7373
invariant(
74-
typeof children === 'function',
75-
"Route children should be a template");
76-
return {path: props.path, children: children};
74+
typeof props.handler === 'function' || typeof handler === 'function',
75+
"Route handler should be a template");
76+
return {path: props.path, handler: props.handler || handler};
7777
}
7878

79-
function NotFound(_props, children) {
79+
function NotFound(props, handler) {
8080
invariant(
81-
typeof children === 'function',
82-
"NotFound children should be a template");
83-
return {path: null, children: children};
81+
typeof props.handler === 'function' || typeof handler === 'function',
82+
"NotFound handler should be a template");
83+
return {path: null, handler: props.handler || handler};
8484
}
8585

8686
module.exports = {

0 commit comments

Comments
 (0)