Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions packages/react-debug-tools/src/ReactDebugHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,8 @@ function useInsertionEffect(
}

function useEffect(
create: (() => (() => void) | void) | (() => {...} | void | null),
createDeps: Array<mixed> | void | null,
update?: ((resource: {...} | void | null) => void) | void,
updateDeps?: Array<mixed> | void | null,
destroy?: ((resource: {...} | void | null) => void) | void,
create: () => (() => void) | void,
deps: Array<mixed> | void | null,
): void {
nextHook();
hookLog.push({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,52 +653,6 @@ describe('ReactDOMServerHooks', () => {
});
});

describe('useEffect with CRUD overload', () => {
gate(flags => {
if (flags.enableUseEffectCRUDOverload) {
const yields = [];
itRenders(
'should ignore resource effects on the server',
async render => {
function Counter(props) {
useEffect(
() => {
yieldValue('created on client');
return {resource_counter: props.count};
},
[props.count],
resource => {
resource.resource_counter = props.count;
yieldValue('updated on client');
},
[props.count],
() => {
yieldValue('cleanup on client');
},
);
return <Text text={'Count: ' + props.count} />;
}

const domNode = await render(<Counter count={0} />);
yields.push(clearLog());
expect(domNode.tagName).toEqual('SPAN');
expect(domNode.textContent).toEqual('Count: 0');
},
);

it('verifies yields in order', () => {
expect(yields).toEqual([
['Count: 0'], // server render
['Count: 0'], // server stream
['Count: 0', 'created on client'], // clean render
['Count: 0', 'created on client'], // hydrated render
// nothing yielded for bad markup
]);
});
}
});
});

describe('useContext', () => {
itThrowsWhenRendering(
'if used inside a class component',
Expand Down
55 changes: 5 additions & 50 deletions packages/react-reconciler/src/ReactFiberCallUserSpace.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import type {CapturedValue} from './ReactCapturedValue';

import {isRendering, setIsRendering} from './ReactCurrentFiber';
import {captureCommitPhaseError} from './ReactFiberWorkLoop';
import {
ResourceEffectIdentityKind,
ResourceEffectUpdateKind,
} from './ReactFiberHooks';
import {enableUseEffectCRUDOverload} from 'shared/ReactFeatureFlags';

// These indirections exists so we can exclude its stack frame in DEV (and anything below it).
// TODO: Consider marking the whole bundle instead of these boundaries.
Expand Down Expand Up @@ -184,51 +179,11 @@ const callCreate = {
'react-stack-bottom-frame': function (
effect: Effect,
): (() => void) | {...} | void | null {
if (!enableUseEffectCRUDOverload) {
if (effect.resourceKind != null) {
if (__DEV__) {
console.error(
'Expected only SimpleEffects when enableUseEffectCRUDOverload is disabled, ' +
'got %s',
effect.resourceKind,
);
}
}
const create = effect.create;
const inst = effect.inst;
// $FlowFixMe[not-a-function] (@poteto)
const destroy = create();
// $FlowFixMe[incompatible-type] (@poteto)
inst.destroy = destroy;
return destroy;
} else {
if (effect.resourceKind == null) {
const create = effect.create;
const inst = effect.inst;
const destroy = create();
inst.destroy = destroy;
return destroy;
}
switch (effect.resourceKind) {
case ResourceEffectIdentityKind: {
return effect.create();
}
case ResourceEffectUpdateKind: {
if (typeof effect.update === 'function') {
effect.update(effect.inst.resource);
}
break;
}
default: {
if (__DEV__) {
console.error(
'Unhandled Effect kind %s. This is a bug in React.',
effect.kind,
);
}
}
}
}
const create = effect.create;
const inst = effect.inst;
const destroy = create();
inst.destroy = destroy;
return destroy;
},
};

Expand Down
132 changes: 7 additions & 125 deletions packages/react-reconciler/src/ReactFiberCommitEffects.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
enableProfilerCommitHooks,
enableProfilerNestedUpdatePhase,
enableSchedulingProfiler,
enableUseEffectCRUDOverload,
enableViewTransition,
enableFragmentRefs,
} from 'shared/ReactFeatureFlags';
Expand Down Expand Up @@ -62,7 +61,6 @@ import {
Layout as HookLayout,
Insertion as HookInsertion,
Passive as HookPassive,
HasEffect as HookHasEffect,
} from './ReactHookEffectTags';
import {didWarnAboutReassigningProps} from './ReactFiberBeginWork';
import {
Expand All @@ -84,10 +82,6 @@ import {
} from './ReactFiberCallUserSpace';

import {runWithFiberInDEV} from './ReactCurrentFiber';
import {
ResourceEffectIdentityKind,
ResourceEffectUpdateKind,
} from './ReactFiberHooks';

function shouldProfile(current: Fiber): boolean {
return (
Expand Down Expand Up @@ -164,91 +158,19 @@ export function commitHookEffectListMount(

// Mount
let destroy;
if (enableUseEffectCRUDOverload) {
if (effect.resourceKind === ResourceEffectIdentityKind) {
if (__DEV__) {
effect.inst.resource = runWithFiberInDEV(
finishedWork,
callCreateInDEV,
effect,
);
if (effect.inst.resource == null) {
console.error(
'useEffect must provide a callback which returns a resource. ' +
'If a managed resource is not needed here, do not provide an updater or ' +
'destroy callback. Received %s',
effect.inst.resource,
);
}
} else {
effect.inst.resource = effect.create();
}
destroy = effect.inst.destroy;
}
if (effect.resourceKind === ResourceEffectUpdateKind) {
if (
// We don't want to fire updates on remount during Activity
(flags & HookHasEffect) > 0 &&
typeof effect.update === 'function' &&
effect.inst.resource != null
) {
// TODO(@poteto) what about multiple updates?
if (__DEV__) {
runWithFiberInDEV(finishedWork, callCreateInDEV, effect);
} else {
effect.update(effect.inst.resource);
}
}
}
}
if (__DEV__) {
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(true);
}
if (enableUseEffectCRUDOverload) {
if (effect.resourceKind == null) {
destroy = runWithFiberInDEV(
finishedWork,
callCreateInDEV,
effect,
);
}
} else {
destroy = runWithFiberInDEV(
finishedWork,
callCreateInDEV,
effect,
);
}
destroy = runWithFiberInDEV(finishedWork, callCreateInDEV, effect);
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(false);
}
} else {
if (enableUseEffectCRUDOverload) {
if (effect.resourceKind == null) {
const create = effect.create;
const inst = effect.inst;
destroy = create();
inst.destroy = destroy;
}
} else {
if (effect.resourceKind != null) {
if (__DEV__) {
console.error(
'Expected only SimpleEffects when enableUseEffectCRUDOverload is disabled, ' +
'got %s',
effect.resourceKind,
);
}
}
const create = effect.create;
const inst = effect.inst;
// $FlowFixMe[incompatible-type] (@poteto)
// $FlowFixMe[not-a-function] (@poteto)
destroy = create();
// $FlowFixMe[incompatible-type] (@poteto)
inst.destroy = destroy;
}
const create = effect.create;
const inst = effect.inst;
destroy = create();
inst.destroy = destroy;
}

if (enableSchedulingProfiler) {
Expand Down Expand Up @@ -338,13 +260,7 @@ export function commitHookEffectListUnmount(
const inst = effect.inst;
const destroy = inst.destroy;
if (destroy !== undefined) {
if (enableUseEffectCRUDOverload) {
if (effect.resourceKind == null) {
inst.destroy = undefined;
}
} else {
inst.destroy = undefined;
}
inst.destroy = undefined;
if (enableSchedulingProfiler) {
if ((flags & HookPassive) !== NoHookEffect) {
markComponentPassiveEffectUnmountStarted(finishedWork);
Expand All @@ -358,41 +274,7 @@ export function commitHookEffectListUnmount(
setIsRunningInsertionEffect(true);
}
}
if (enableUseEffectCRUDOverload) {
if (
effect.resourceKind === ResourceEffectIdentityKind &&
effect.inst.resource != null
) {
safelyCallDestroy(
finishedWork,
nearestMountedAncestor,
destroy,
effect.inst.resource,
);
if (effect.next.resourceKind === ResourceEffectUpdateKind) {
// $FlowFixMe[prop-missing] (@poteto)
effect.next.update = undefined;
} else {
if (__DEV__) {
console.error(
'Expected a ResourceEffectUpdateKind to follow ResourceEffectIdentityKind, ' +
'got %s. This is a bug in React.',
effect.next.resourceKind,
);
}
}
effect.inst.resource = null;
}
if (effect.resourceKind == null) {
safelyCallDestroy(
finishedWork,
nearestMountedAncestor,
destroy,
);
}
} else {
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
}
safelyCallDestroy(finishedWork, nearestMountedAncestor, destroy);
if (__DEV__) {
if ((flags & HookInsertion) !== NoHookEffect) {
setIsRunningInsertionEffect(false);
Expand Down
Loading
Loading