Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,17 @@ export function Router(props) {

let pathRoute, defaultRoute, matchProps;
toChildArray(props.children).some((/** @type {VNode<any>} */ vnode) => {
const matches = exec(rest, vnode.props.path, (matchProps = { ...vnode.props, path: rest, query, params, rest: '' }));
const matches = exec(
rest,
vnode.props.path,
(matchProps = {
...vnode.props,
path: rest,
query,
params: Object.assign({}, params),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Object.assign() is the only change, rest is just formatting as that line was getting rather long & hard to read.

rest: ''
})
);
if (matches) return (pathRoute = cloneElement(vnode, matchProps));
if (vnode.props.default) defaultRoute = cloneElement(vnode, matchProps);
});
Expand Down
36 changes: 36 additions & 0 deletions test/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,42 @@ describe('Router', () => {
expect(Shadow).to.have.been.calledOnce;
expect(scratch).to.have.property('textContent', 'Shadow Route');
});

it('should not preserve param state after match failures', async () => {
const Params = () => {
const { params } = useRoute();
return <h1>{JSON.stringify(params)}</h1>
};

render(
<LocationProvider>
<Router>
<Route path="/category/:id" component={Params} />
<Route path="/category/:categoryId/products/new" component={Params} />
<Route path="/category/:categoryId/products/:id/edit" component={Params} />
</Router>
<ShallowLocation />
</LocationProvider>,
scratch
);

loc.route('/category/123');
await sleep(10);

expect(scratch).to.have.property('textContent', '{"id":"123"}');

loc.route('/category/123/products/new');
await sleep(10);

// If the same `params` object was reused, this would also have an `id` property
// from a failed partial match against the first route.
expect(scratch).to.have.property('textContent', '{"categoryId":"123"}');

loc.route('/category/123/products/456/edit');
await sleep(10);

expect(scratch).to.have.property('textContent', '{"categoryId":"123","id":"456"}');
});
});

const MODE_HYDRATE = 1 << 5;
Expand Down