I want my component know if some library is already loaded. To know that from any context i connect it to the "library" reducer of my store to my component.
I also pass it a configuration object this.props.dataObject
from the parent where the component has been called. Like this:
class GoogleButton extends Component {
render() {
if (this.props.libraries.google) {
return <a id='sharePost' className='google_icon'></a>
} else {
return null
}
}
componentDidUpdate() {
gapi.interactivepost.render('sharePost', this.props.dataObject)
}
}
function mapStateToProps(state) {
return { libraries: state.libraries }
}
export default connect(mapStateToProps)(GoogleButton)
The reducer that handles the libraries state is like this:
let newState = {...state}
newState[action.libraryName] = action.state
return newState
When I change the library state componentDidUpdate
works. The problem is when i change the prop inherited by the parent this.props.dataObject
. In that case is where componentDidUpdate wont fire. If i remove the connect
from the component it works as espected. I'm missing something here?
Most likely some of your props are mutated outside the component.
For example, you might be rendering your component like this:
Because Redux apps deal with immutable data,
connect()
uses shallow equality check for its props to avoid unnecessary re-renders. However, this won’t work if you use mutation in your app.You have two options:
Don’t Mutate Anything
This is the best option. For example, instead of something like
you can write
This way we are creating a new object so
connect()
wouldn’t ignore the changed reference. (I’m using the object spread syntax in this snippet.)Disable Performance Optimizations
A worse alternative is to completely disable performance optimizations made by
connect()
. Then your props would update even if you mutate them in the parent, but your app will be slower. To do this, replacewith
Don’t do this unless absolutely necessary.
i had same problem and i used object.assign for create new state but i use combineReducer and it cause multi level state ,in my case i pass whole state as props to component so
shallow equality check
can not detect my state change so componentDidUpdate didnot call,it is important to pass state in level it change when using combine reducer in my case i pass it like thisand my state tree is like this
and in my reducer and return it like this as
ReportReducer
and my root reducer is like this
I solved it. I'm not 100% sure that this is accurate, but I will explain. If im wrong with something, please correct me.
I keep thinking about the shallow equality check that Dan said in his answer. The problem was there. I was passing down an object from the parent and the nested elements of that object were the ones that changed. The object remain the same. So with the shallow equality check that connect brings the component will never update.
My solution was in the parent use
Object.assign({}, dataObject)
when I pass down the prop so I make another different object. Now shallow equality check could compare it and determinate that the props have changed and there before update the component.