Skip to content

Commit

Permalink
Improves props validation
Browse files Browse the repository at this point in the history
Resolves brave#8557

Auditors: @bridiver

Test Plan:
- create redux component
- pass prop as a simple List
- component should not update if props is the same
  • Loading branch information
NejcZdovc committed Apr 28, 2017
1 parent d9745a5 commit b0c4e46
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/common/state/immutableUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const api = {
return Immutable.List.isList(obj)
},

isSame: (first, second) => {
return Immutable.is(first, second)
},

makeImmutable: (obj) => {
return api.isImmutable(obj) ? obj : Immutable.fromJS(obj)
}
Expand Down
13 changes: 11 additions & 2 deletions app/renderer/components/reduxComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const ImmutableComponent = require('./immutableComponent')
const React = require('react')
const windowStore = require('../../../js/stores/windowStore')
const debounce = require('../../../js/lib/debounce')
const {isList, isSame} = require('../../common/state/immutableUtil')

const mergePropsImpl = (stateProps, dispatchProps, ownProps) => {
return Object.assign({}, stateProps, dispatchProps, ownProps)
Expand Down Expand Up @@ -45,8 +46,16 @@ class ReduxComponent extends ImmutableComponent {
}

shouldComponentUpdate (nextProps, nextState) {
return Object.keys(nextState).some((prop) => nextState[prop] !== this.state[prop]) ||
Object.keys(nextProps).some((prop) => nextProps[prop] !== this.props[prop])
return Object.keys(nextState).some((prop) => {
return isList(nextState[prop])
? !isSame(nextState[prop], this.state[prop])
: nextState[prop] !== this.state[prop]
}) ||
Object.keys(nextProps).some((prop) => {
return isList(nextProps[prop])
? !isSame(nextProps[prop], this.props[prop])
: nextProps[prop] !== this.props[prop]
})
}

mergeProps (stateProps, dispatchProps, ownProps) {
Expand Down

0 comments on commit b0c4e46

Please sign in to comment.