Skip to content

Commit

Permalink
Fix the bug by restoring currentlyRenderingFiber even if the reducer …
Browse files Browse the repository at this point in the history
…throws

I introduced this in facebook#14653 (223960e). But I relied on restart to clean it up in case of error, and didn't notice the try/catch. This makes sure the failing case also restores the current variable which renderWitHooks() currently relies on.
  • Loading branch information
gaearon committed Jan 23, 2019
1 parent 9e4aec2 commit b567b68
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1054,14 +1054,16 @@ function dispatchAction<S, A>(
// same as the current state, we may be able to bail out entirely.
const eagerReducer = queue.eagerReducer;
if (eagerReducer !== null) {
// Note: this may be null and is *not* necessarily the `fiber`.
// In particular, they would be different if one component dispatches
// on another component in the render phase.
let savedCurrentlyRenderingFiber = currentlyRenderingFiber;
try {
const currentState: S = (queue.eagerState: any);
// Temporarily clear to forbid calling Hooks in a reducer.
let maybeFiber = currentlyRenderingFiber; // Note: likely null now unlike `fiber`
currentlyRenderingFiber = null;
stashContextDependencies();
const eagerState = eagerReducer(currentState, action);
currentlyRenderingFiber = maybeFiber;
unstashContextDependencies();
// Stash the eagerly computed state, and the reducer used to compute
// it, on the update object. If the reducer hasn't changed by the
Expand All @@ -1078,6 +1080,8 @@ function dispatchAction<S, A>(
}
} catch (error) {
// Suppress the error. It will throw again in the render phase.
} finally {
currentlyRenderingFiber = savedCurrentlyRenderingFiber;
}
}
}
Expand Down

0 comments on commit b567b68

Please sign in to comment.