Skip to content

Commit 59266f5

Browse files
committed
Use existing DEV reset mechanism
1 parent b58e231 commit 59266f5

File tree

5 files changed

+85
-31
lines changed

5 files changed

+85
-31
lines changed

packages/react-reconciler/src/ReactFiberCommitWork.new.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -537,15 +537,16 @@ function commitHookEffectListUnmount(
537537
}
538538
}
539539

540-
if (__DEV__ && (flags & HookInsertion) !== NoHookEffect) {
541-
setIsRunningInsertionEffect(true);
542-
try {
543-
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
544-
} finally {
540+
if (__DEV__) {
541+
if ((flags & HookInsertion) !== NoHookEffect) {
542+
setIsRunningInsertionEffect(true);
543+
}
544+
}
545+
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
546+
if (__DEV__) {
547+
if ((flags & HookInsertion) !== NoHookEffect) {
545548
setIsRunningInsertionEffect(false);
546549
}
547-
} else {
548-
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
549550
}
550551

551552
if (enableSchedulingProfiler) {
@@ -580,15 +581,16 @@ function commitHookEffectListMount(flags: HookFlags, finishedWork: Fiber) {
580581

581582
// Mount
582583
const create = effect.create;
583-
if (__DEV__ && (flags & HookInsertion) !== NoHookEffect) {
584-
setIsRunningInsertionEffect(true);
585-
try {
586-
effect.destroy = create();
587-
} finally {
584+
if (__DEV__) {
585+
if ((flags & HookInsertion) !== NoHookEffect) {
586+
setIsRunningInsertionEffect(true);
587+
}
588+
}
589+
effect.destroy = create();
590+
if (__DEV__) {
591+
if ((flags & HookInsertion) !== NoHookEffect) {
588592
setIsRunningInsertionEffect(false);
589593
}
590-
} else {
591-
effect.destroy = create();
592594
}
593595

594596
if (enableSchedulingProfiler) {

packages/react-reconciler/src/ReactFiberCommitWork.old.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -537,15 +537,16 @@ function commitHookEffectListUnmount(
537537
}
538538
}
539539

540-
if (__DEV__ && (flags & HookInsertion) !== NoHookEffect) {
541-
setIsRunningInsertionEffect(true);
542-
try {
543-
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
544-
} finally {
540+
if (__DEV__) {
541+
if ((flags & HookInsertion) !== NoHookEffect) {
542+
setIsRunningInsertionEffect(true);
543+
}
544+
}
545+
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
546+
if (__DEV__) {
547+
if ((flags & HookInsertion) !== NoHookEffect) {
545548
setIsRunningInsertionEffect(false);
546549
}
547-
} else {
548-
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
549550
}
550551

551552
if (enableSchedulingProfiler) {
@@ -580,15 +581,16 @@ function commitHookEffectListMount(flags: HookFlags, finishedWork: Fiber) {
580581

581582
// Mount
582583
const create = effect.create;
583-
if (__DEV__ && (flags & HookInsertion) !== NoHookEffect) {
584-
setIsRunningInsertionEffect(true);
585-
try {
586-
effect.destroy = create();
587-
} finally {
584+
if (__DEV__) {
585+
if ((flags & HookInsertion) !== NoHookEffect) {
586+
setIsRunningInsertionEffect(true);
587+
}
588+
}
589+
effect.destroy = create();
590+
if (__DEV__) {
591+
if ((flags & HookInsertion) !== NoHookEffect) {
588592
setIsRunningInsertionEffect(false);
589593
}
590-
} else {
591-
effect.destroy = create();
592594
}
593595

594596
if (enableSchedulingProfiler) {

packages/react-reconciler/src/ReactFiberWorkLoop.new.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2531,6 +2531,9 @@ export function captureCommitPhaseError(
25312531
nearestMountedAncestor: Fiber | null,
25322532
error: mixed,
25332533
) {
2534+
if (__DEV__) {
2535+
setIsRunningInsertionEffect(false);
2536+
}
25342537
if (sourceFiber.tag === HostRoot) {
25352538
// Error was thrown at the root. There is no parent, so the root
25362539
// itself should capture it.

packages/react-reconciler/src/ReactFiberWorkLoop.old.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2531,6 +2531,9 @@ export function captureCommitPhaseError(
25312531
nearestMountedAncestor: Fiber | null,
25322532
error: mixed,
25332533
) {
2534+
if (__DEV__) {
2535+
setIsRunningInsertionEffect(false);
2536+
}
25342537
if (sourceFiber.tag === HostRoot) {
25352538
// Error was thrown at the root. There is no parent, so the root
25362539
// itself should capture it.

packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3096,7 +3096,10 @@ describe('ReactHooksWithNoopRenderer', () => {
30963096
const [, setX] = useState(0);
30973097
useInsertionEffect(() => {
30983098
setX(1);
3099-
}, []);
3099+
if (props.throw) {
3100+
throw Error('No');
3101+
}
3102+
}, [props.throw]);
31003103
return null;
31013104
}
31023105

@@ -3106,14 +3109,37 @@ describe('ReactHooksWithNoopRenderer', () => {
31063109
root.render(<App />);
31073110
}),
31083111
).toErrorDev(['Warning: useInsertionEffect must not schedule updates.']);
3112+
3113+
expect(() => {
3114+
act(() => {
3115+
root.render(<App throw={true} />);
3116+
});
3117+
}).toThrow('No');
3118+
3119+
// Should not warn for regular effects after throw.
3120+
function NotInsertion() {
3121+
const [, setX] = useState(0);
3122+
useEffect(() => {
3123+
setX(1);
3124+
}, []);
3125+
return null;
3126+
}
3127+
act(() => {
3128+
root.render(<NotInsertion />);
3129+
});
31093130
});
31103131

31113132
it('warns when setState is called from insertion effect cleanup', () => {
31123133
function App(props) {
31133134
const [, setX] = useState(0);
31143135
useInsertionEffect(() => {
3115-
return () => setX(1);
3116-
}, [props.foo]);
3136+
if (props.throw) {
3137+
throw Error('No');
3138+
}
3139+
return () => {
3140+
setX(1);
3141+
};
3142+
}, [props.throw, props.foo]);
31173143
return null;
31183144
}
31193145

@@ -3126,6 +3152,24 @@ describe('ReactHooksWithNoopRenderer', () => {
31263152
root.render(<App foo="goodbye" />);
31273153
});
31283154
}).toErrorDev(['Warning: useInsertionEffect must not schedule updates.']);
3155+
3156+
expect(() => {
3157+
act(() => {
3158+
root.render(<App throw={true} />);
3159+
});
3160+
}).toThrow('No');
3161+
3162+
// Should not warn for regular effects after throw.
3163+
function NotInsertion() {
3164+
const [, setX] = useState(0);
3165+
useEffect(() => {
3166+
setX(1);
3167+
}, []);
3168+
return null;
3169+
}
3170+
act(() => {
3171+
root.render(<NotInsertion />);
3172+
});
31293173
});
31303174
});
31313175

0 commit comments

Comments
 (0)