From 57cd762262c2f8f70a9511304a074ae92d8fa986 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Tue, 21 Sep 2021 18:24:16 +0300 Subject: [PATCH] Get key from item (not props), do not update state if there is no change --- virtualRender.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/virtualRender.js b/virtualRender.js index d410b48..0d35a4f 100644 --- a/virtualRender.js +++ b/virtualRender.js @@ -2,7 +2,7 @@ // Simpler alternative to react-test-renderer, also allows to visit element tree during rendering // // (c) Vitaliy Filippov 2021+ -// Version: 2021-09-20 +// Version: 2021-09-21 // License: Dual-license MPL 1.1+ or GNU LGPL 3.0+ // Credits to react-apollo/getDataFromTree.ts and react-tree-walker @@ -92,7 +92,7 @@ function walkTree(tree, visitor, context, options, path = '') for (let item of tree) { let type = item && getType(item); - let key = item && getProps(item)?.key; + let key = item && item.key; if (lastType == type && lastHasKey == (key != null)) index++; else @@ -197,8 +197,15 @@ function walkTree(tree, visitor, context, options, path = '') // eslint-disable-next-line no-param-reassign newState = newState(instance.state, instance.props, instance.context); } - instance.state = { ...instance.state, ...newState }; - options.shouldUpdate = true; + for (let k in newState) + { + if (instance.state[k] !== newState[k]) + { + instance.state = { ...instance.state, ...newState }; + options.shouldUpdate = true; + break; + } + } if (callback) { callback();