Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #5114 warn about history prop in Router-derived components #5151

Merged
merged 7 commits into from
Jun 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/react-router-dom/modules/BrowserRouter.js
Expand Up @@ -18,6 +18,12 @@ class BrowserRouter extends React.Component {
history = createHistory(this.props)

render() {
if (this.props.history) {
console.error(
'`<BrowserRouter history={...}` prop has been ignored. For custom history, ' +
'make sure to `import {Router}` and not `import {... as Router}`.')
}

Copy link
Contributor

@pshrmn pshrmn May 23, 2017

Choose a reason for hiding this comment

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

This should just use the warning package. You'll need to install it for react-router-dom, but it is already installed for react-router. Basically, just change this block to:

warning(
  !this.props.history,
  ...
);

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a minor nit, but I would prefer if there were spaces inside of the curly brackets. import { Router } and import { ... as Router }.

return <Router history={this.history} children={this.props.children}/>
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/react-router-dom/modules/HashRouter.js
Expand Up @@ -17,6 +17,12 @@ class HashRouter extends React.Component {
history = createHistory(this.props)

render() {
if (this.props.history) {
console.error(
'`<HashRouter history={...}` prop has been ignored. For custom history, ' +
'make sure to `import {Router}` and not `import {... as Router}`.')
}

return <Router history={this.history} children={this.props.children}/>
}
}
Expand Down
15 changes: 15 additions & 0 deletions packages/react-router-dom/modules/__tests__/BrowserRouter-test.js
Expand Up @@ -26,4 +26,19 @@ describe('A <BrowserRouter>', () => {

expect(history).toBeAn('object')
})

it('warns when passed a history prop', () => {
const history = {}
const node = document.createElement('div')
expect.spyOn(console, 'error')

ReactDOM.render((
<BrowserRouter history={history} />
), node)

expect(console.error.calls.length).toBe(1)
expect(console.error.calls[0].arguments[0]).toMatch(
/<BrowserRouter.*import \{Router}/)
expect.restoreSpies()
})
})
15 changes: 15 additions & 0 deletions packages/react-router-dom/modules/__tests__/HashRouter-test.js
Expand Up @@ -26,4 +26,19 @@ describe('A <HashRouter>', () => {

expect(history).toBeAn('object')
})

it('warns when passed a history prop', () => {
const history = {}
const node = document.createElement('div')
expect.spyOn(console, 'error')

ReactDOM.render((
<HashRouter history={history} />
), node)

expect(console.error.calls.length).toBe(1)
expect(console.error.calls[0].arguments[0]).toMatch(
/<HashRouter.*import \{Router}/)
expect.restoreSpies()
})
})
6 changes: 6 additions & 0 deletions packages/react-router/modules/MemoryRouter.js
Expand Up @@ -18,6 +18,12 @@ class MemoryRouter extends React.Component {
history = createHistory(this.props)

render() {
if (this.props.history) {
console.error(
'`<MemoryRouter history={...}` prop has been ignored. For custom history, ' +
'make sure to `import {Router}` and not `import {... as Router}`.')
}

return <Router history={this.history} children={this.props.children}/>
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/react-router/modules/StaticRouter.js
Expand Up @@ -127,6 +127,12 @@ class StaticRouter extends React.Component {
block: this.handleBlock
}

if (this.props.history) {
console.error(
'`<StaticRouter history={...}` prop has been ignored. For custom history, ' +
'make sure to `import {Router}` and not `import {... as Router}`.')
}

return <Router {...props} history={history}/>
}
}
Expand Down
15 changes: 15 additions & 0 deletions packages/react-router/modules/__tests__/MemoryRouter-test.js
Expand Up @@ -26,4 +26,19 @@ describe('A <MemoryRouter>', () => {

expect(history).toBeAn('object')
})

it('warns when passed a history prop', () => {
const history = {}
const node = document.createElement('div')
expect.spyOn(console, 'error')

ReactDOM.render((
<MemoryRouter history={history} />
), node)

expect(console.error.calls.length).toBe(1)
expect(console.error.calls[0].arguments[0]).toMatch(
/<MemoryRouter.*import \{Router}/)
expect.restoreSpies()
})
})
17 changes: 17 additions & 0 deletions packages/react-router/modules/__tests__/StaticRouter-test.js
Expand Up @@ -68,6 +68,22 @@ describe('A <StaticRouter>', () => {
)
})

it('warns when passed a history prop', () => {
const context = {}
const history = {}
const node = document.createElement('div')
expect.spyOn(console, 'error')

ReactDOM.render((
<StaticRouter context={context} history={history} />
), node)

expect(console.error.calls.length).toBe(1)
expect(console.error.calls[0].arguments[0]).toMatch(
/<StaticRouter.*import \{Router}/)
expect.restoreSpies()
})

it('reports PUSH actions on the context object', () => {
const context = {}

Expand Down Expand Up @@ -211,4 +227,5 @@ describe('A <StaticRouter>', () => {
}).toNotThrow()
})
})

})