Skip to content

Commit d9a5dcf

Browse files
authored
Merge pull request #11 from preactjs/allow-passing-props-to-routes
feat: allow passing props to routes
2 parents 763f6cb + abdeb3d commit d9a5dcf

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export function Router(props) {
128128

129129
let p, d, m;
130130
toChildArray(props.children).some(vnode => {
131-
const matches = exec(rest, vnode.props.path, (m = { path: rest, query, params, rest: '' }));
131+
const matches = exec(rest, vnode.props.path, (m = { ...vnode.props, path: rest, query, params, rest: '' }));
132132
if (matches) return (p = cloneElement(vnode, m));
133133
if (vnode.props.default) d = cloneElement(vnode, m);
134134
});

test/router.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,38 @@ describe('Router', () => {
2323
history.replaceState(null, null, '/');
2424
});
2525

26+
it('should allow passing props to a route', async () => {
27+
const Home = jest.fn(() => html`<h1>Home</h1>`);
28+
const stack = [];
29+
let loc;
30+
render(
31+
html`
32+
<${LocationProvider}>
33+
<${Router}
34+
onRouteChange=${url => {
35+
stack.push(url);
36+
}}
37+
>
38+
<${Home} path="/" test="2" />
39+
<//>
40+
<${() => {
41+
loc = useLocation();
42+
}} />
43+
<//>
44+
`,
45+
scratch
46+
);
47+
48+
expect(scratch).toHaveProperty('textContent', 'Home');
49+
expect(Home).toHaveBeenCalledWith({ path: '/', query: {}, params: {}, rest: '', test: '2' }, expect.anything());
50+
expect(loc).toMatchObject({
51+
url: '/',
52+
path: '/',
53+
query: {},
54+
route: expect.any(Function)
55+
});
56+
});
57+
2658
it('should switch between synchronous routes', async () => {
2759
const Home = jest.fn(() => html`<h1>Home</h1>`);
2860
const Profiles = jest.fn(() => html`<h1>Profiles</h1>`);

0 commit comments

Comments
 (0)