Skip to content

Commit

Permalink
Fix hook values not updated during forced update
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Mar 2, 2023
1 parent f20dca0 commit 3d1ade9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 17 additions & 1 deletion hooks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,23 @@ export function useReducer(reducer, initialState, init) {

if (!currentComponent._hasScuFromHooks) {
currentComponent._hasScuFromHooks = true;
const prevScu = currentComponent.shouldComponentUpdate;
let prevScu = currentComponent.shouldComponentUpdate;
const prevCWU = currentComponent.componentWillUpdate;

// If we're dealing with a forced update `shouldComponentUpdate` will
// not be called. But we use that to update the hook values, so we
// need to call it.
currentComponent.componentWillUpdate = function(p, s, c) {
if (this._force) {
let tmp = prevScu;
// Clear to avoid other sCU hooks from being called
prevScu = undefined;
this.shouldComponentUpdate(p, s, c);
prevScu = tmp;
}

if (prevCWU) prevCWU.call(this, p, s, c);
};

// This SCU has the purpose of bailing out after repeated updates
// to stateful hooks.
Expand Down
4 changes: 1 addition & 3 deletions src/create-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ export function createContext(defaultValue, contextId) {
// enqueueRender(c);
// });
subs.some(c => {
if (!c._hasScuFromHooks) {
c._force = true;
}
c._force = true;
enqueueRender(c);
});
}
Expand Down

0 comments on commit 3d1ade9

Please sign in to comment.