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

Move onLocationChanged to componentDidMount #217

Merged
merged 1 commit into from Dec 27, 2018
Merged
Changes from all 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
20 changes: 10 additions & 10 deletions src/ConnectedRouter.js
Expand Up @@ -15,31 +15,31 @@ const createConnectedRouter = (structure) => {
*/

class ConnectedRouter extends PureComponent {
constructor(props) {
super(props)
componentDidMount() {
const { store, history, onLocationChanged } = this.props

this.inTimeTravelling = false

// Subscribe to store changes
this.unsubscribe = props.store.subscribe(() => {
// Subscribe to store changes to check if we are in time travelling
this.unsubscribe = store.subscribe(() => {
// Extract store's location
const {
pathname: pathnameInStore,
search: searchInStore,
hash: hashInStore,
} = getLocation(props.store.getState())
} = getLocation(store.getState())
// Extract history's location
const {
pathname: pathnameInHistory,
search: searchInHistory,
hash: hashInHistory,
} = props.history.location
} = history.location

// If we do time travelling, the location in store is changed but location in history is not changed
if (pathnameInHistory !== pathnameInStore || searchInHistory !== searchInStore || hashInHistory !== hashInStore) {
this.inTimeTravelling = true
// Update history's location to match store's location
props.history.push({
history.push({
pathname: pathnameInStore,
search: searchInStore,
hash: hashInStore,
Expand All @@ -50,16 +50,16 @@ const createConnectedRouter = (structure) => {
const handleLocationChange = (location, action) => {
// Dispatch onLocationChanged except when we're in time travelling
if (!this.inTimeTravelling) {
props.onLocationChanged(location, action)
onLocationChanged(location, action)
} else {
this.inTimeTravelling = false
}
}

// Listen to history changes
this.unlisten = props.history.listen(handleLocationChange)
this.unlisten = history.listen(handleLocationChange)
// Dispatch a location change action for the initial location
handleLocationChange(props.history.location, props.history.action)
handleLocationChange(history.location, history.action)
}

componentWillUnmount() {
Expand Down