From b0c4e46bb0cd6b6138a1c69ad2f6c2639d4cc458 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 | 4 ++++ app/renderer/components/reduxComponent.js | 13 +++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/common/state/immutableUtil.js b/app/common/state/immutableUtil.js index ae1c90b7651..ecbf1111c4b 100644 --- a/app/common/state/immutableUtil.js +++ b/app/common/state/immutableUtil.js @@ -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) } diff --git a/app/renderer/components/reduxComponent.js b/app/renderer/components/reduxComponent.js index 40716ee173e..5e08bb4163d 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, isSame} = 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]) + ? !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) {