Skip to content

Commit 63598b0

Browse files
authored
Make initial location POP optional (#398)
1 parent 9b3ef9b commit 63598b0

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

FAQ.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,15 @@ ReactDOM.render(
353353
</Provider>
354354
)
355355
```
356+
357+
### How to stop initial location change
358+
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.
359+
```js
360+
ReactDOM.render(
361+
<Provider store={store}>
362+
<ConnectedRouter history={history} noInitialPop>
363+
<Route path="/" component={myComponent} exact={true} />
364+
</ConnectedRouter>
365+
</Provider>
366+
)
367+
```

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ declare module 'connected-react-router' {
1717
interface ConnectedRouterProps<S = LocationState> {
1818
history: History<S>;
1919
context?: React.Context<ReactReduxContextValue>;
20+
noInitialPop?: boolean;
2021
}
2122

2223
export type RouterActionType = Action;

src/ConnectedRouter.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,13 @@ const createConnectedRouter = (structure) => {
6969

7070
// Listen to history changes
7171
this.unlisten = history.listen(handleLocationChange)
72-
// Dispatch a location change action for the initial location.
73-
// This makes it backward-compatible with react-router-redux.
74-
// But, we add `isFirstRendering` to `true` to prevent double-rendering.
75-
handleLocationChange(history.location, history.action, true)
72+
73+
if (!props.noInitialPop) {
74+
// Dispatch a location change action for the initial location.
75+
// This makes it backward-compatible with react-router-redux.
76+
// But, we add `isFirstRendering` to `true` to prevent double-rendering.
77+
handleLocationChange(history.location, history.action, true)
78+
}
7679
}
7780

7881
componentWillUnmount() {
@@ -105,6 +108,7 @@ const createConnectedRouter = (structure) => {
105108
basename: PropTypes.string,
106109
children: PropTypes.oneOfType([ PropTypes.func, PropTypes.node ]),
107110
onLocationChanged: PropTypes.func.isRequired,
111+
noInitialPop: PropTypes.bool,
108112
}
109113

110114
const mapDispatchToProps = dispatch => ({

test/ConnectedRouter.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ describe('ConnectedRouter', () => {
191191
history.push('/new-location')
192192
expect(renderCount).toBe(2)
193193
})
194+
195+
it('does not call `props.onLocationChanged()` on intial location when `noInitialPop` prop is passed ', () => {
196+
mount(
197+
<Provider store={store}>
198+
<ConnectedRouter {...props} noInitialPop>
199+
<Route path="/" render={() => <div>Home</div>} />
200+
</ConnectedRouter>
201+
</Provider>
202+
)
203+
204+
expect(onLocationChangedSpy.mock.calls).toHaveLength(0)
205+
})
194206
})
195207

196208
describe('with immutable structure', () => {

0 commit comments

Comments
 (0)