Skip to content

Commit b78aee4

Browse files
committed
Pathless child routes inherit parent, even when null
This is only an issue when you use <Route children>. If you render a pathless <Route> inside of a <Route children>, you must also use <Route children> (instead of <Route render> or <Route component>). This commit also switches withRouter to using <Route children> instead of <Route render> so that it works when the parent match is null.
1 parent 9004194 commit b78aee4

File tree

5 files changed

+9
-24
lines changed

5 files changed

+9
-24
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ describe('A pathless <Route>', () => {
394394
rootContext = undefined
395395
})
396396

397-
it("inherits its parent match", () => {
397+
it('inherits its parent match', () => {
398398
const node = document.createElement('div')
399399
ReactDOM.render((
400400
<MemoryRouter initialEntries={[ '/somepath' ]}>
@@ -409,7 +409,7 @@ describe('A pathless <Route>', () => {
409409
expect(match.params).toEqual({})
410410
})
411411

412-
it('computes match with default values when parent match is null', () => {
412+
it('does not render when parent match is null', () => {
413413
const node = document.createElement('div')
414414
ReactDOM.render((
415415
<MemoryRouter initialEntries={[ '/somepath' ]}>
@@ -418,11 +418,6 @@ describe('A pathless <Route>', () => {
418418
)}/>
419419
</MemoryRouter>
420420
), node)
421-
const { match } = rootContext.router.route
422-
423-
expect(match.path).toBe(undefined)
424-
expect(match.url).toBe('/_FAKE_MATCH_DO_NOT_ATTEMPT_TO_RESOLVE_USING_THIS_OR_YOU_WILL_BE_DISAPPOINTED')
425-
expect(match.isExact).toBe(true)
426-
expect(match.params).toEqual({})
421+
expect(rootContext).toBe(undefined)
427422
})
428423
})

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,10 @@ describe('matchPath', () => {
6666
expect(match).toBe(parentMatch)
6767
})
6868

69-
it('returns match with default values when parent match is null', () => {
69+
it('returns null when parent match is null', () => {
7070
const pathname = '/some/path'
7171
const match = matchPath(pathname, {}, null)
72-
expect(match.url).toBe('/_FAKE_MATCH_DO_NOT_ATTEMPT_TO_RESOLVE_USING_THIS_OR_YOU_WILL_BE_DISAPPOINTED')
73-
expect(match.path).toBe(undefined)
74-
expect(match.params).toEqual({})
75-
expect(match.isExact).toBe(true)
72+
expect(match).toBe(null)
7673
})
7774
})
7875

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,13 @@ describe('withRouter', () => {
6262
<MemoryRouter initialEntries={[ '/somepath' ]}>
6363
<Route path='/no-match' children={({ match }) => {
6464
parentMatch = match
65-
return <Route component={WrappedPropChecker} />
65+
return <WrappedPropChecker />
6666
}}/>
6767
</MemoryRouter>
6868
), node)
6969

70-
const { match } = injectedProps
7170
expect(parentMatch).toBe(null)
72-
expect(match).not.toBe(null)
71+
expect(injectedProps.match).toBe(null)
7372
})
7473

7574
describe('inside a <StaticRouter>', () => {

packages/react-router/modules/matchPath.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ const matchPath = (pathname, options = {}, parent) => {
3333
const { path, exact = false, strict = false, sensitive = false } = options
3434

3535
if (path == null)
36-
return parent != null
37-
? parent
38-
: {
39-
url: '/_FAKE_MATCH_DO_NOT_ATTEMPT_TO_RESOLVE_USING_THIS_OR_YOU_WILL_BE_DISAPPOINTED',
40-
isExact: true,
41-
params: {}
42-
}
36+
return parent
4337

4438
const { re, keys } = compilePath(path, { end: exact, strict, sensitive })
4539
const match = re.exec(pathname)

packages/react-router/modules/withRouter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const withRouter = (Component) => {
1010
const C = (props) => {
1111
const { wrappedComponentRef, ...remainingProps } = props
1212
return (
13-
<Route render={routeComponentProps => (
13+
<Route children={routeComponentProps => (
1414
<Component {...remainingProps} {...routeComponentProps} ref={wrappedComponentRef}/>
1515
)}/>
1616
)

0 commit comments

Comments
 (0)