Skip to content

Commit

Permalink
update hashFunction on state shape change
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Sep 16, 2018
1 parent b018b98 commit 129ad67
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/components/Provider.js
@@ -1,6 +1,7 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Context, { createHashFunction } from './Context'
import shallowEqual from '../utils/shallowEqual'

const ContextProvider = Context.Provider

Expand Down Expand Up @@ -32,13 +33,21 @@ class Provider extends Component {
}

componentDidUpdate(lastProps) {
if (lastProps.store !== this.props.store) {
const state = this.props.store.getState()
const storesEqual = lastProps.store === this.props.store
if (!storesEqual) {
if (this.unsubscribe) this.unsubscribe()
this.unsubscribe = this.props.store.subscribe(this.triggerUpdateOnStoreStateChange.bind(this))
const state = this.props.store.getState()
this.setState({
state,
store: this.props.store,
store: this.props.store
})
}
if(
!storesEqual ||
!shallowEqual(Object.keys(state), Object.keys(lastProps.store.getState()))
) {
this.setState({
hashFunction: createHashFunction(state)
})
}
Expand Down

0 comments on commit 129ad67

Please sign in to comment.