Skip to content

Commit f90d753

Browse files
committed
Remove trailing parent argument to matchPath
Also, be more explicit about the options we pass to matchPath. Instead of allowing both `path` and `from`, do the coercion ahead of time in <Switch>.
1 parent 92b2ab3 commit f90d753

File tree

4 files changed

+19
-30
lines changed

4 files changed

+19
-30
lines changed

packages/react-router/modules/Route.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Route extends React.Component {
4848

4949
const pathname = (location || route.location).pathname
5050

51-
return matchPath(pathname, { path, strict, exact }, route.match)
51+
return path ? matchPath(pathname, { path, strict, exact }) : route.match
5252
}
5353

5454
componentWillReceiveProps(nextProps, nextContext) {

packages/react-router/modules/Switch.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@ class Switch extends React.Component {
2828
}
2929

3030
render() {
31+
const { route } = this.context
3132
const { children } = this.props
32-
const location = this.props.location || this.context.route.location
33-
const parent = this.context.route.match
33+
const location = this.props.location || route.location
34+
3435
let match, child
3536
React.Children.forEach(children, element => {
37+
const { path: pathProp, exact, strict, from } = element.props
38+
const path = pathProp || from
39+
3640
if (match == null) {
3741
child = element
38-
match = matchPath(location.pathname, element.props, parent)
42+
match = path ? matchPath(location.pathname, { path, exact, strict }) : route.match
3943
}
4044
})
4145

packages/react-router/modules/__tests__/matchPath-test.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,15 @@ describe('matchPath', () => {
3434
})
3535
})
3636

37-
describe("with no path", () => {
38-
it("returns parent match", () => {
39-
const parentMatch = {
40-
url: '/test-location/7',
41-
path: '/test-location/:number',
42-
params: { number: 7 },
43-
isExact: true
44-
}
45-
const match = matchPath('/test-location/7', {}, parentMatch)
46-
expect(match).toBe(parentMatch)
47-
})
48-
49-
it('returns match with default values when parent match is null', () => {
50-
const pathname = '/some/path'
51-
const match = matchPath(pathname, {}, null)
52-
expect(match.url).toBe(pathname)
53-
expect(match.path).toBe(undefined)
54-
expect(match.params).toEqual({})
55-
expect(match.isExact).toBe(true)
37+
describe('with no path', () => {
38+
it('matches the root URL', () => {
39+
const match = matchPath('/test-location/7', {})
40+
expect(match).toMatch({
41+
url: '/',
42+
path: '/',
43+
params: {},
44+
isExact: false
45+
})
5646
})
5747
})
5848

packages/react-router/modules/matchPath.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,11 @@ const compilePath = (pattern, options) => {
2626
/**
2727
* Public API for matching a URL pathname to a path pattern.
2828
*/
29-
const matchPath = (pathname, options = {}, parent) => {
29+
const matchPath = (pathname, options = {}) => {
3030
if (typeof options === 'string')
3131
options = { path: options }
3232

33-
const { exact = false, strict = false } = options
34-
const path = options.path || options.from
35-
36-
if (!path)
37-
return parent != null ? parent : { url: pathname, isExact: true, params: {} }
38-
33+
const { path = '/', exact = false, strict = false } = options
3934
const { re, keys } = compilePath(path, { end: exact, strict })
4035
const match = re.exec(pathname)
4136

0 commit comments

Comments
 (0)