Skip to content

Commit 6648a2f

Browse files
Aprilliontimdorr
authored andcommitted
Issue remix-run#5114 warn about history prop in Router-derived components (remix-run#5151)
* Issue remix-run#5114 warn about history prop in Router-derived components * Use warning module * Clean up BrowserRouter warning * Clean up HashRouter warning * Clean up MemoryRouter warning * Clean up StaticRouter warning
1 parent c6ccf65 commit 6648a2f

File tree

8 files changed

+90
-0
lines changed

8 files changed

+90
-0
lines changed

packages/react-router-dom/modules/BrowserRouter.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warning from 'warning'
12
import React from 'react'
23
import PropTypes from 'prop-types'
34
import createHistory from 'history/createBrowserHistory'
@@ -18,6 +19,12 @@ class BrowserRouter extends React.Component {
1819
history = createHistory(this.props)
1920

2021
render() {
22+
warning(
23+
!this.props.history,
24+
'<BrowserRouter> ignores the history prop. To use a custom history, ' +
25+
'make sure you are using `import { Router }` and not `import { BrowserRouter as Router }`.'
26+
)
27+
2128
return <Router history={this.history} children={this.props.children}/>
2229
}
2330
}

packages/react-router-dom/modules/HashRouter.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warning from 'warning'
12
import React from 'react'
23
import PropTypes from 'prop-types'
34
import createHistory from 'history/createHashHistory'
@@ -17,6 +18,12 @@ class HashRouter extends React.Component {
1718
history = createHistory(this.props)
1819

1920
render() {
21+
warning(
22+
!this.props.history,
23+
'<HashRouter> ignores the history prop. To use a custom history, ' +
24+
'make sure you are using `import { Router }` and not `import { HashRouter as Router }`.'
25+
)
26+
2027
return <Router history={this.history} children={this.props.children}/>
2128
}
2229
}

packages/react-router-dom/modules/__tests__/BrowserRouter-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,19 @@ describe('A <BrowserRouter>', () => {
2626

2727
expect(history).toBeAn('object')
2828
})
29+
30+
it('warns when passed a history prop', () => {
31+
const history = {}
32+
const node = document.createElement('div')
33+
expect.spyOn(console, 'error')
34+
35+
ReactDOM.render((
36+
<BrowserRouter history={history} />
37+
), node)
38+
39+
expect(console.error.calls.length).toBe(1)
40+
expect(console.error.calls[0].arguments[0]).toMatch(
41+
/<BrowserRouter.*import \{ Router }/)
42+
expect.restoreSpies()
43+
})
2944
})

packages/react-router-dom/modules/__tests__/HashRouter-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,19 @@ describe('A <HashRouter>', () => {
2626

2727
expect(history).toBeAn('object')
2828
})
29+
30+
it('warns when passed a history prop', () => {
31+
const history = {}
32+
const node = document.createElement('div')
33+
expect.spyOn(console, 'error')
34+
35+
ReactDOM.render((
36+
<HashRouter history={history} />
37+
), node)
38+
39+
expect(console.error.calls.length).toBe(1)
40+
expect(console.error.calls[0].arguments[0]).toMatch(
41+
/<HashRouter.*import \{ Router }/)
42+
expect.restoreSpies()
43+
})
2944
})

packages/react-router/modules/MemoryRouter.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warning from 'warning'
12
import React from 'react'
23
import PropTypes from 'prop-types'
34
import createHistory from 'history/createMemoryHistory'
@@ -18,6 +19,12 @@ class MemoryRouter extends React.Component {
1819
history = createHistory(this.props)
1920

2021
render() {
22+
warning(
23+
!this.props.history,
24+
'<MemoryRouter> ignores the history prop. To use a custom history, ' +
25+
'make sure you are using `import { Router }` and not `import { MemoryRouter as Router }`.'
26+
)
27+
2128
return <Router history={this.history} children={this.props.children}/>
2229
}
2330
}

packages/react-router/modules/StaticRouter.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warning from 'warning'
12
import invariant from 'invariant'
23
import React from 'react'
34
import PropTypes from 'prop-types'
@@ -127,6 +128,12 @@ class StaticRouter extends React.Component {
127128
block: this.handleBlock
128129
}
129130

131+
warning(
132+
!this.props.history,
133+
'<StaticRouter> ignores the history prop. To use a custom history, ' +
134+
'make sure you are using `import { Router }` and not `import { StaticRouter as Router }`.'
135+
)
136+
130137
return <Router {...props} history={history}/>
131138
}
132139
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,19 @@ describe('A <MemoryRouter>', () => {
2626

2727
expect(history).toBeAn('object')
2828
})
29+
30+
it('warns when passed a history prop', () => {
31+
const history = {}
32+
const node = document.createElement('div')
33+
expect.spyOn(console, 'error')
34+
35+
ReactDOM.render((
36+
<MemoryRouter history={history} />
37+
), node)
38+
39+
expect(console.error.calls.length).toBe(1)
40+
expect(console.error.calls[0].arguments[0]).toMatch(
41+
/<MemoryRouter.*import \{ Router }/)
42+
expect.restoreSpies()
43+
})
2944
})

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ describe('A <StaticRouter>', () => {
6868
)
6969
})
7070

71+
it('warns when passed a history prop', () => {
72+
const context = {}
73+
const history = {}
74+
const node = document.createElement('div')
75+
expect.spyOn(console, 'error')
76+
77+
ReactDOM.render((
78+
<StaticRouter context={context} history={history} />
79+
), node)
80+
81+
expect(console.error.calls.length).toBe(1)
82+
expect(console.error.calls[0].arguments[0]).toMatch(
83+
/<StaticRouter.*import \{ Router }/)
84+
expect.restoreSpies()
85+
})
86+
7187
it('reports PUSH actions on the context object', () => {
7288
const context = {}
7389

@@ -211,4 +227,5 @@ describe('A <StaticRouter>', () => {
211227
}).toNotThrow()
212228
})
213229
})
230+
214231
})

0 commit comments

Comments
 (0)