Skip to content

Commit

Permalink
Make initial location POP optional (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
dr3 authored Mar 14, 2020
1 parent 9b3ef9b commit 63598b0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
12 changes: 12 additions & 0 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,15 @@ ReactDOM.render(
</Provider>
)
```

### How to stop initial location change
In order to make this package more compatible with react-router-redux, a LOCATION_CHANGE action is dispatched for the initial location. This can however be disabled via the `noInitialPop` prop.
```js
ReactDOM.render(
<Provider store={store}>
<ConnectedRouter history={history} noInitialPop>
<Route path="/" component={myComponent} exact={true} />
</ConnectedRouter>
</Provider>
)
```
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ declare module 'connected-react-router' {
interface ConnectedRouterProps<S = LocationState> {
history: History<S>;
context?: React.Context<ReactReduxContextValue>;
noInitialPop?: boolean;
}

export type RouterActionType = Action;
Expand Down
12 changes: 8 additions & 4 deletions src/ConnectedRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ const createConnectedRouter = (structure) => {

// Listen to history changes
this.unlisten = history.listen(handleLocationChange)
// Dispatch a location change action for the initial location.
// This makes it backward-compatible with react-router-redux.
// But, we add `isFirstRendering` to `true` to prevent double-rendering.
handleLocationChange(history.location, history.action, true)

if (!props.noInitialPop) {
// Dispatch a location change action for the initial location.
// This makes it backward-compatible with react-router-redux.
// But, we add `isFirstRendering` to `true` to prevent double-rendering.
handleLocationChange(history.location, history.action, true)
}
}

componentWillUnmount() {
Expand Down Expand Up @@ -105,6 +108,7 @@ const createConnectedRouter = (structure) => {
basename: PropTypes.string,
children: PropTypes.oneOfType([ PropTypes.func, PropTypes.node ]),
onLocationChanged: PropTypes.func.isRequired,
noInitialPop: PropTypes.bool,
}

const mapDispatchToProps = dispatch => ({
Expand Down
12 changes: 12 additions & 0 deletions test/ConnectedRouter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@ describe('ConnectedRouter', () => {
history.push('/new-location')
expect(renderCount).toBe(2)
})

it('does not call `props.onLocationChanged()` on intial location when `noInitialPop` prop is passed ', () => {
mount(
<Provider store={store}>
<ConnectedRouter {...props} noInitialPop>
<Route path="/" render={() => <div>Home</div>} />
</ConnectedRouter>
</Provider>
)

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

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

0 comments on commit 63598b0

Please sign in to comment.