[Perf] Teact: Avoid redundant renders when updating state with a callback

This commit is contained in:
Alexander Zinchuk 2023-01-22 18:12:37 +01:00
parent 6213ed60d5
commit c6bd762a4c

View File

@ -473,10 +473,15 @@ export function useState<T>(initial?: T, debugKey?: string): [T, StateHookSetter
value: initial, value: initial,
nextValue: initial, nextValue: initial,
setter: ((componentInstance) => (newValue: ((current: T) => T) | T) => { setter: ((componentInstance) => (newValue: ((current: T) => T) | T) => {
if (byCursor[cursor].nextValue !== newValue) { if (typeof newValue === 'function') {
byCursor[cursor].nextValue = typeof newValue === 'function' newValue = (newValue as (current: T) => T)(byCursor[cursor].value);
? (newValue as (current: T) => T)(byCursor[cursor].value) }
: newValue;
if (byCursor[cursor].nextValue === newValue) {
return;
}
byCursor[cursor].nextValue = newValue;
if (!componentInstance.prepareForFrame || !componentInstance.forceUpdate) { if (!componentInstance.prepareForFrame || !componentInstance.forceUpdate) {
componentInstance.prepareForFrame = throttleWithPrimaryRaf( componentInstance.prepareForFrame = throttleWithPrimaryRaf(
@ -500,14 +505,11 @@ export function useState<T>(initial?: T, debugKey?: string): [T, StateHookSetter
componentInstance.Component && (componentInstance.Component as FC_withDebug).DEBUG_contentComponentName componentInstance.Component && (componentInstance.Component as FC_withDebug).DEBUG_contentComponentName
? `> ${(componentInstance.Component as FC_withDebug).DEBUG_contentComponentName}` ? `> ${(componentInstance.Component as FC_withDebug).DEBUG_contentComponentName}`
: '', : '',
debugKey `State update at cursor #${cursor}${debugKey ? ` (${debugKey})` : ''}, next value: `,
? `State update for ${debugKey}, next value: `
: `State update at cursor #${cursor}, next value: `,
byCursor[cursor].nextValue, byCursor[cursor].nextValue,
); );
} }
} }
}
})(renderingInstance), })(renderingInstance),
}; };
} }