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

Prevent remount component on routes with the same component #5430

Merged
Show file tree
Hide file tree
Changes from 2 commits
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
@@ -1,7 +1,10 @@
import React from 'react'
import ReactDOM from 'react-dom'
import ReactDOMServer from 'react-dom/server'
import StaticRouter from 'react-router/StaticRouter'
import Router from 'react-router/Router'
import renderRoutes from '../renderRoutes'
import createHistory from 'history/createMemoryHistory'

describe('renderRoutes', () => {
let renderedRoutes
Expand Down Expand Up @@ -111,6 +114,58 @@ describe('renderRoutes', () => {
expect(renderedRoutes[0]).toEqual(routeToMatch)
expect(renderedRoutes[1]).toEqual(childRouteToMatch)
})

it('does not remount a <Route>', () => {
const node = document.createElement('div')

let mountCount = 0

const App = ({ route: { routes } }) => (
renderRoutes(routes)
)

class Comp extends React.Component {
componentDidMount() {
mountCount++
}

render() {
return <div />
}
}

const routes = [
{ path: '/',
component: App,
routes: [
{ path: '/one',
component: Comp
},
{ path: '/two',
component: Comp
}
]
}
]

const history = createHistory({
initialEntries: [ '/one' ]
})

ReactDOM.render((
<Router history={history}>
{renderRoutes(routes)}
</Router>
), node)

expect(mountCount).toBe(1)

history.push('/one')
expect(mountCount).toBe(1)

history.push('/two')
expect(mountCount).toBe(1)
})
})

describe('routes with exact', () => {
Expand Down
14 changes: 10 additions & 4 deletions packages/react-router-config/modules/renderRoutes.js
Expand Up @@ -4,10 +4,16 @@ import Route from 'react-router/Route'

const renderRoutes = (routes, extraProps = {}) => routes ? (
<Switch>
{routes.map((route, i) => (
<Route key={i} path={route.path} exact={route.exact} strict={route.strict} render={(props) => (
<route.component {...props} {...extraProps} route={route}/>
)}/>
{routes.map(route => (
<Route
key='prevent-remount-and-use-static-key'
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you make this shorter? Seems a waste to put a long string here just to remind devs. key='fixed' would also work, no?

path={route.path}
exact={route.exact}
strict={route.strict}
render={(props) => (
<route.component {...props} {...extraProps} route={route}/>
)}
/>
))}
</Switch>
) : null
Expand Down
1 change: 1 addition & 0 deletions packages/react-router-config/package.json
Expand Up @@ -41,6 +41,7 @@
"eslint-plugin-import": "^1.15.0",
"eslint-plugin-react": "^5.2.2",
"gzip-size": "^3.0.0",
"history": "^4.6.3",
"jest": "^20.0.4",
"pretty-bytes": "^3.0.1",
"react": "^15.4.2",
Expand Down