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 ConnectedRouter from re-rendering on every redux store update #208

Merged
merged 6 commits into from Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions src/ConnectedRouter.js
@@ -1,4 +1,4 @@
import React, { Component } from 'react'
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import { connect, ReactReduxContext } from 'react-redux'
import { Router } from 'react-router'
Expand All @@ -15,7 +15,7 @@ const createConnectedRouter = (structure) => {
* This creates uni-directional flow from history->store->router->components.
*/

class ConnectedRouter extends Component {
class ConnectedRouter extends PureComponent {
constructor(props) {
super(props)

Expand Down
62 changes: 59 additions & 3 deletions test/ConnectedRouter.test.js
Expand Up @@ -2,7 +2,7 @@ import 'raf/polyfill'
import React, { Children, Component } from 'react'
import PropTypes from 'prop-types'
import configureStore from 'redux-mock-store'
import { createStore, combineReducers } from 'redux'
import { createStore, combineReducers, applyMiddleware, compose } from 'redux'
import { ActionCreators, instrument } from 'redux-devtools'
import Enzyme from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
Expand All @@ -14,7 +14,7 @@ import { onLocationChanged } from '../src/actions'
import plainStructure from '../src/structure/plain'
import immutableStructure from '../src/structure/immutable'
import seamlessImmutableStructure from '../src/structure/seamless-immutable'
import { connectRouter, ConnectedRouter } from '../src'
import { connectRouter, ConnectedRouter, routerMiddleware } from '../src'

Enzyme.configure({ adapter: new Adapter() })

Expand Down Expand Up @@ -123,7 +123,63 @@ describe('ConnectedRouter', () => {

expect(onLocationChangedSpy.mock.calls).toHaveLength(3)
})
})

it('only renders one time when mounted', () => {
let renderCount = 0

const RenderCounter = () => {
renderCount++
return null
}

mount(
<MockProvider store={store}>
<ConnectedRouter {...props}>
<Route path="/" component={RenderCounter} />
</ConnectedRouter>
</MockProvider>
)

expect(renderCount).toBe(1)
})

it('does not render again when non-related action is fired', () => {
// Initialize the render counter variable
let renderCount = 0

// Create redux store with router state
store = createStore(
combineReducers({
incrementReducer: (state = 0, action = {}) => {
if (action.type === 'testAction')
return ++state

return state
},
router: connectRouter(history)
}),
compose(applyMiddleware(routerMiddleware(history)))
)


const RenderCounter = () => {
renderCount++
return null
}

mount(
<MockProvider store={store}>
<ConnectedRouter {...props}>
<Route path="/" component={RenderCounter} />
</ConnectedRouter>
</MockProvider>
)

store.dispatch({ type: 'testAction' })
history.push('/new-location')
expect(renderCount).toBe(2)
})

Choose a reason for hiding this comment

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

@AnnaTsu Just so I understand what's going on here, store.dispatch() will not re-render RenderCounter, BUT history.push() will, correct? Therefore renderCount should be 2.

initial render + history.push() = 2 renders

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly. Initial render times upon mount should be one.

Then, i fire an action that has no connection to what ConnectedRouter should listen to, and modify the history (something that the component should re-render).

So, the expected render times should be 2.

})

describe('with immutable structure', () => {
let ConnectedRouter
Expand Down