Skip to content

Commit

Permalink
Fix hooks calling shouldComponentUpdate without context (#3671)
Browse files Browse the repository at this point in the history
* Fix hooks calling shouldComponentUpdate without context

This was breaking code that relies on `this` within shouldComponentUpdate.

* Fix `this` usage
  • Loading branch information
developit authored Aug 16, 2022
1 parent c9ad4f3 commit 4441dc7
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions hooks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 4441dc7

Please sign in to comment.