Skip to content

Commit 394eb09

Browse files
pshrmnmjackson
authored andcommitted
Pathless <Route> inherits parent match (#4643)
When the parent match is null, a new match object is created where the url is the full pathname and isExact is true
1 parent b7357d1 commit 394eb09

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
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 })
51+
return matchPath(pathname, { path, strict, exact }, route.match)
5252
}
5353

5454
componentWillReceiveProps(nextProps, nextContext) {

packages/react-router/modules/Switch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ class Switch extends React.Component {
3030
render() {
3131
const { children } = this.props
3232
const location = this.props.location || this.context.route.location
33-
33+
const parent = this.context.route.match
3434
let match, child
3535
React.Children.forEach(children, element => {
3636
if (match == null) {
3737
child = element
38-
match = matchPath(location.pathname, element.props)
38+
match = matchPath(location.pathname, element.props, parent)
3939
}
4040
})
4141

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ 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)
56+
})
57+
})
58+
3759
describe('cache', () => {
3860
it('creates a cache entry for each exact/strict pair', () => {
3961
// true/false and false/true will collide when adding booleans

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,21 @@ describe('withRouter', () => {
2828
</MemoryRouter>
2929
), node)
3030
})
31+
32+
it('passes parent match to wrapped component', () => {
33+
let parentMatch
34+
const ContextChecker = withRouter(props => {
35+
expect(props.match).toEqual(parentMatch)
36+
return null
37+
})
38+
39+
ReactDOM.render((
40+
<MemoryRouter initialEntries={[ '/bubblegum' ]}>
41+
<Route path="/:flavor" render={({ match }) => {
42+
parentMatch = match
43+
return <ContextChecker/>
44+
}}/>
45+
</MemoryRouter>
46+
), node)
47+
})
3148
})

packages/react-router/modules/matchPath.js

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

3333
const { exact = false, strict = false } = options
3434
const path = options.path || options.from
3535

3636
if (!path)
37-
return { url: pathname, isExact: true, params: {} }
37+
return parent != null ? parent : { url: pathname, isExact: true, params: {} }
3838

3939
const { re, keys } = compilePath(path, { end: exact, strict })
4040
const match = re.exec(pathname)

0 commit comments

Comments
 (0)