From 2686d23e1746d8be708ef5ea63150a0a3bbacc89 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Fri, 28 Apr 2017 21:12:07 +0200 Subject: [PATCH] Improves props validation Resolves #8557 Auditors: @bridiver Test Plan: - create redux component - pass prop as a simple List - component should not update if props is the same --- app/common/state/immutableUtil.js | 8 ++++++++ app/renderer/components/reduxComponent.js | 13 +++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/common/state/immutableUtil.js b/app/common/state/immutableUtil.js index ae1c90b7651..5d3cb25fbd3 100644 --- a/app/common/state/immutableUtil.js +++ b/app/common/state/immutableUtil.js @@ -17,6 +17,14 @@ const api = { return Immutable.List.isList(obj) }, + isSameHashCode: (first, second) => { + if (first === null && second === null) { + return true + } + + return second !== null ? first.hashCode() === second.hashCode() : false + }, + makeImmutable: (obj) => { return api.isImmutable(obj) ? obj : Immutable.fromJS(obj) } diff --git a/app/renderer/components/reduxComponent.js b/app/renderer/components/reduxComponent.js index 40716ee173e..ba917d8f1bd 100644 --- a/app/renderer/components/reduxComponent.js +++ b/app/renderer/components/reduxComponent.js @@ -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, isSameHashCode} = require('../../common/state/immutableUtil') const mergePropsImpl = (stateProps, dispatchProps, ownProps) => { return Object.assign({}, stateProps, dispatchProps, ownProps) @@ -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]) + ? !isSameHashCode(nextState[prop], this.state[prop]) + : nextState[prop] !== this.state[prop] + }) || + Object.keys(nextProps).some((prop) => { + return isList(nextProps[prop]) + ? !isSameHashCode(nextProps[prop], this.props[prop]) + : nextProps[prop] !== this.props[prop] + }) } mergeProps (stateProps, dispatchProps, ownProps) {