[Perf] Teact: Avoid redundant renders when updating state with a callback
This commit is contained in:
parent
6213ed60d5
commit
c6bd762a4c
@ -473,10 +473,15 @@ export function useState<T>(initial?: T, debugKey?: string): [T, StateHookSetter
|
||||
value: initial,
|
||||
nextValue: initial,
|
||||
setter: ((componentInstance) => (newValue: ((current: T) => T) | T) => {
|
||||
if (byCursor[cursor].nextValue !== newValue) {
|
||||
byCursor[cursor].nextValue = typeof newValue === 'function'
|
||||
? (newValue as (current: T) => T)(byCursor[cursor].value)
|
||||
: newValue;
|
||||
if (typeof newValue === 'function') {
|
||||
newValue = (newValue as (current: T) => T)(byCursor[cursor].value);
|
||||
}
|
||||
|
||||
if (byCursor[cursor].nextValue === newValue) {
|
||||
return;
|
||||
}
|
||||
|
||||
byCursor[cursor].nextValue = newValue;
|
||||
|
||||
if (!componentInstance.prepareForFrame || !componentInstance.forceUpdate) {
|
||||
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 as FC_withDebug).DEBUG_contentComponentName}`
|
||||
: '',
|
||||
debugKey
|
||||
? `State update for ${debugKey}, next value: `
|
||||
: `State update at cursor #${cursor}, next value: `,
|
||||
`State update at cursor #${cursor}${debugKey ? ` (${debugKey})` : ''}, next value: `,
|
||||
byCursor[cursor].nextValue,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
})(renderingInstance),
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user