Get key from item (not props), do not update state if there is no change

master
Vitaliy Filippov 2021-09-21 18:24:16 +03:00
parent 4f72118fe8
commit 57cd762262
1 changed files with 11 additions and 4 deletions

View File

@ -2,7 +2,7 @@
// Simpler alternative to react-test-renderer, also allows to visit element tree during rendering // Simpler alternative to react-test-renderer, also allows to visit element tree during rendering
// //
// (c) Vitaliy Filippov 2021+ // (c) Vitaliy Filippov 2021+
// Version: 2021-09-20 // Version: 2021-09-21
// License: Dual-license MPL 1.1+ or GNU LGPL 3.0+ // License: Dual-license MPL 1.1+ or GNU LGPL 3.0+
// Credits to react-apollo/getDataFromTree.ts and react-tree-walker // 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) for (let item of tree)
{ {
let type = item && getType(item); let type = item && getType(item);
let key = item && getProps(item)?.key; let key = item && item.key;
if (lastType == type && lastHasKey == (key != null)) if (lastType == type && lastHasKey == (key != null))
index++; index++;
else else
@ -197,8 +197,15 @@ function walkTree(tree, visitor, context, options, path = '')
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
newState = newState(instance.state, instance.props, instance.context); newState = newState(instance.state, instance.props, instance.context);
} }
instance.state = { ...instance.state, ...newState }; for (let k in newState)
options.shouldUpdate = true; {
if (instance.state[k] !== newState[k])
{
instance.state = { ...instance.state, ...newState };
options.shouldUpdate = true;
break;
}
}
if (callback) if (callback)
{ {
callback(); callback();