Skip to content

Commit

Permalink
Use existing DEV reset mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Apr 7, 2022
1 parent b58e231 commit 59266f5
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 31 deletions.
30 changes: 16 additions & 14 deletions packages/react-reconciler/src/ReactFiberCommitWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,15 +537,16 @@ function commitHookEffectListUnmount(
}
}

if (__DEV__ && (flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(true);
try {
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
} finally {
if (__DEV__) {
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(true);
}
}
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
if (__DEV__) {
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(false);
}
} else {
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
}

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

// Mount
const create = effect.create;
if (__DEV__ && (flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(true);
try {
effect.destroy = create();
} finally {
if (__DEV__) {
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(true);
}
}
effect.destroy = create();
if (__DEV__) {
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(false);
}
} else {
effect.destroy = create();
}

if (enableSchedulingProfiler) {
Expand Down
30 changes: 16 additions & 14 deletions packages/react-reconciler/src/ReactFiberCommitWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,15 +537,16 @@ function commitHookEffectListUnmount(
}
}

if (__DEV__ && (flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(true);
try {
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
} finally {
if (__DEV__) {
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(true);
}
}
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
if (__DEV__) {
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(false);
}
} else {
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
}

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

// Mount
const create = effect.create;
if (__DEV__ && (flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(true);
try {
effect.destroy = create();
} finally {
if (__DEV__) {
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(true);
}
}
effect.destroy = create();
if (__DEV__) {
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(false);
}
} else {
effect.destroy = create();
}

if (enableSchedulingProfiler) {
Expand Down
3 changes: 3 additions & 0 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2531,6 +2531,9 @@ export function captureCommitPhaseError(
nearestMountedAncestor: Fiber | null,
error: mixed,
) {
if (__DEV__) {
setIsRunningInsertionEffect(false);
}
if (sourceFiber.tag === HostRoot) {
// Error was thrown at the root. There is no parent, so the root
// itself should capture it.
Expand Down
3 changes: 3 additions & 0 deletions packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -2531,6 +2531,9 @@ export function captureCommitPhaseError(
nearestMountedAncestor: Fiber | null,
error: mixed,
) {
if (__DEV__) {
setIsRunningInsertionEffect(false);
}
if (sourceFiber.tag === HostRoot) {
// Error was thrown at the root. There is no parent, so the root
// itself should capture it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3096,7 +3096,10 @@ describe('ReactHooksWithNoopRenderer', () => {
const [, setX] = useState(0);
useInsertionEffect(() => {
setX(1);
}, []);
if (props.throw) {
throw Error('No');
}
}, [props.throw]);
return null;
}

Expand All @@ -3106,14 +3109,37 @@ describe('ReactHooksWithNoopRenderer', () => {
root.render(<App />);
}),
).toErrorDev(['Warning: useInsertionEffect must not schedule updates.']);

expect(() => {
act(() => {
root.render(<App throw={true} />);
});
}).toThrow('No');

// Should not warn for regular effects after throw.
function NotInsertion() {
const [, setX] = useState(0);
useEffect(() => {
setX(1);
}, []);
return null;
}
act(() => {
root.render(<NotInsertion />);
});
});

it('warns when setState is called from insertion effect cleanup', () => {
function App(props) {
const [, setX] = useState(0);
useInsertionEffect(() => {
return () => setX(1);
}, [props.foo]);
if (props.throw) {
throw Error('No');
}
return () => {
setX(1);
};
}, [props.throw, props.foo]);
return null;
}

Expand All @@ -3126,6 +3152,24 @@ describe('ReactHooksWithNoopRenderer', () => {
root.render(<App foo="goodbye" />);
});
}).toErrorDev(['Warning: useInsertionEffect must not schedule updates.']);

expect(() => {
act(() => {
root.render(<App throw={true} />);
});
}).toThrow('No');

// Should not warn for regular effects after throw.
function NotInsertion() {
const [, setX] = useState(0);
useEffect(() => {
setX(1);
}, []);
return null;
}
act(() => {
root.render(<NotInsertion />);
});
});
});

Expand Down

0 comments on commit 59266f5

Please sign in to comment.