From 4441dc799227d15c5b8488cb34147b5230ebcbf8 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 16 Aug 2022 04:26:03 -0400 Subject: [PATCH] Fix hooks calling shouldComponentUpdate without context (#3671) * Fix hooks calling shouldComponentUpdate without context This was breaking code that relies on `this` within shouldComponentUpdate. * Fix `this` usage --- hooks/src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hooks/src/index.js b/hooks/src/index.js index d838d17356..b0fb376d5e 100644 --- a/hooks/src/index.js +++ b/hooks/src/index.js @@ -184,7 +184,7 @@ export function useReducer(reducer, initialState, init) { if (!currentComponent._hasScuFromHooks) { currentComponent._hasScuFromHooks = true; const prevScu = currentComponent.shouldComponentUpdate; - currentComponent.shouldComponentUpdate = (p, s, c) => { + currentComponent.shouldComponentUpdate = function (p, s, c) { if (!hookState._component.__hooks) return true; const stateHooks = hookState._component.__hooks._list.filter( x => x._component @@ -193,7 +193,7 @@ export function useReducer(reducer, initialState, init) { // When we have no updated hooks in the component we invoke the previous SCU or // traverse the VDOM tree further. if (allHooksEmpty) { - return prevScu ? prevScu(p, s, c) : true; + return prevScu ? prevScu.call(this, p, s, c) : true; } // We check whether we have components with a nextValue set that @@ -208,7 +208,7 @@ export function useReducer(reducer, initialState, init) { }); if (!shouldSkipUpdating) { - return prevScu ? prevScu(p, s, c) : true; + return prevScu ? prevScu.call(this, p, s, c) : true; } // When all set nextValues are equal to their original value