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

Redirect on props update (#5003) #5162

Merged
merged 3 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/react-router/modules/Redirect.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React from 'react'
import PropTypes from 'prop-types'
import warning from 'warning'
import invariant from 'invariant'
import { createLocation, locationsAreEqual } from 'history'

/**
* The public API for updating the location programatically
* The public API for updating the location programmatically
* with a component.
*/
class Redirect extends React.Component {
Expand All @@ -13,7 +15,7 @@ class Redirect extends React.Component {
to: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object
])
]).isRequired
}

static defaultProps = {
Expand Down Expand Up @@ -49,6 +51,19 @@ class Redirect extends React.Component {
this.perform()
}

componentDidUpdate(prevProps) {
const prevTo = createLocation(prevProps.to)
const nextTo = createLocation(this.props.to)

if (locationsAreEqual(prevTo, nextTo)) {
warning(false, `You tried to redirect to the same route you're currently on: ` +
`"${nextTo.pathname}${nextTo.search}"`)
return
}

this.perform()
}

perform() {
const { history } = this.context.router
const { push, to } = this.props
Expand Down
139 changes: 139 additions & 0 deletions packages/react-router/modules/__tests__/Switch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,145 @@ describe('A <Switch>', () => {
expect(node.innerHTML).toContain('bub')
})

it('handles subsequent redirects', () => {
const node = document.createElement('div')

ReactDOM.render((
<MemoryRouter initialEntries={[ '/one' ]}>
<Switch>
<Redirect exact from="/one" to="/two"/>
<Redirect exact from="/two" to="/three"/>

<Route path="/three" render={() => <div>three</div>}/>
</Switch>
</MemoryRouter>
), node)

expect(node.innerHTML).toContain('three')
})

it('warns when redirecting to same route, both strings', () => {
const node = document.createElement('div')
let redirected = false
let done = false

spyOn(console, 'error')

ReactDOM.render((
<MemoryRouter initialEntries={[ '/one' ]}>
<Switch>
<Route path="/one" render={() => {
if (done)
return <h1>done</h1>

if (!redirected) {
return <Redirect to="/one"/>
}
done = true

return <Redirect to='/one'/>
}}/>
</Switch>
</MemoryRouter>
), node)

expect(node.innerHTML).not.toContain('done')
expect(console.error.calls.count()).toBe(1)
expect(console.error.calls.argsFor(0)[0]).toMatch(/Warning:.*"\/one"/)
})

it('warns when redirecting to same route, mixed types', () => {
const node = document.createElement('div')
let redirected = false
let done = false

spyOn(console, 'error')

ReactDOM.render((
<MemoryRouter initialEntries={[ '/one' ]}>
<Switch>
<Route path="/one" render={() => {
if (done)
return <h1>done</h1>

if (!redirected) {
redirected = true
return <Redirect to="/one"/>
}
done = true

return <Redirect to={{ pathname: '/one' }}/>
}}/>
</Switch>
</MemoryRouter>
), node)

expect(node.innerHTML).not.toContain('done')
expect(console.error.calls.count()).toBe(1)
expect(console.error.calls.argsFor(0)[0]).toMatch(/Warning:.*"\/one"/)
})

it('warns when redirecting to same route, mixed types, string with query', () => {
const node = document.createElement('div')
let redirected = false
let done = false

spyOn(console, 'error')

ReactDOM.render((
<MemoryRouter initialEntries={[ '/one' ]}>
<Switch>
<Route path="/one" render={() => {
if (done)
return <h1>done</h1>

if (!redirected) {
redirected = true
return <Redirect to="/one?utm=1"/>
}
done = true

return <Redirect to={{ pathname: '/one', search: '?utm=1' }}/>
}}/>
</Switch>
</MemoryRouter>
), node)

expect(node.innerHTML).not.toContain('done')
expect(console.error.calls.count()).toBe(1)
expect(console.error.calls.argsFor(0)[0]).toMatch(/Warning:.*"\/one\?utm=1"/)
})

it('does NOT warn when redirecting to same route with different `search`', () => {
const node = document.createElement('div')
let redirected = false
let done = false

spyOn(console, 'error')

ReactDOM.render((
<MemoryRouter initialEntries={[ '/one' ]}>
<Switch>
<Route path="/one" render={() => {
if (done)
return <h1>done</h1>

if (!redirected) {
redirected = true
return <Redirect to={{ pathname: '/one', search: '?utm=1' }}/>
}
done = true

return <Redirect to={{ pathname: '/one', search: '?utm=2' }}/>
}}/>
</Switch>
</MemoryRouter>
), node)

expect(node.innerHTML).toContain('done')
expect(console.error.calls.count()).toBe(0)
})

it('handles comments', () => {
const node = document.createElement('div')

Expand Down