diff --git a/packages/react-reconciler/src/ReactFiber.js b/packages/react-reconciler/src/ReactFiber.js
index 78f2568800..ff251cbf68 100644
--- a/packages/react-reconciler/src/ReactFiber.js
+++ b/packages/react-reconciler/src/ReactFiber.js
@@ -299,10 +299,15 @@ function shouldConstruct(Component: Function) {
return !!(prototype && prototype.isReactComponent);
}
-export function resolveLazyComponentTag(
- fiber: Fiber,
- Component: Function,
-): WorkTag {
+export function isSimpleFunctionComponent(type: any) {
+ return (
+ typeof type === 'function' &&
+ !shouldConstruct(type) &&
+ type.defaultProps === undefined
+ );
+}
+
+export function resolveLazyComponentTag(Component: Function): WorkTag {
if (typeof Component === 'function') {
return shouldConstruct(Component) ? ClassComponent : FunctionComponent;
} else if (Component !== undefined && Component !== null) {
@@ -406,20 +411,15 @@ export function createHostRootFiber(isConcurrent: boolean): Fiber {
return createFiber(HostRoot, null, null, mode);
}
-function createFiberFromElementWithoutDebugInfo(
- element: ReactElement,
+export function createFiberFromTypeAndProps(
+ type: any, // React$ElementType
+ key: null | string,
+ pendingProps: any,
+ owner: null | Fiber,
mode: TypeOfMode,
expirationTime: ExpirationTime,
): Fiber {
- let owner = null;
- if (__DEV__) {
- owner = element._owner;
- }
-
let fiber;
- const type = element.type;
- const key = element.key;
- const pendingProps = element.props;
let fiberTag = IndeterminateComponent;
// The resolved type is set if we know what the final type will be. I.e. it's not lazy.
@@ -522,8 +522,18 @@ export function createFiberFromElement(
mode: TypeOfMode,
expirationTime: ExpirationTime,
): Fiber {
- const fiber = createFiberFromElementWithoutDebugInfo(
- element,
+ let owner = null;
+ if (__DEV__) {
+ owner = element._owner;
+ }
+ const type = element.type;
+ const key = element.key;
+ const pendingProps = element.props;
+ const fiber = createFiberFromTypeAndProps(
+ type,
+ key,
+ pendingProps,
+ owner,
mode,
expirationTime,
);
diff --git a/packages/react-reconciler/src/ReactFiberBeginWork.js b/packages/react-reconciler/src/ReactFiberBeginWork.js
index e9304857e4..cd65c864ed 100644
--- a/packages/react-reconciler/src/ReactFiberBeginWork.js
+++ b/packages/react-reconciler/src/ReactFiberBeginWork.js
@@ -31,6 +31,7 @@ import {
Profiler,
SuspenseComponent,
PureComponent,
+ SimplePureComponent,
LazyComponent,
} from 'shared/ReactWorkTags';
import {
@@ -103,8 +104,10 @@ import {
import {readLazyComponentType} from './ReactFiberLazyComponent';
import {
resolveLazyComponentTag,
+ createFiberFromTypeAndProps,
createFiberFromFragment,
createWorkInProgress,
+ isSimpleFunctionComponent,
} from './ReactFiber';
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
@@ -235,19 +238,49 @@ function updatePureComponent(
nextProps: any,
updateExpirationTime,
renderExpirationTime: ExpirationTime,
-) {
- const render = Component.render;
-
+): null | Fiber {
+ if (current === null) {
+ let type = Component.type;
+ if (isSimpleFunctionComponent(type) && Component.compare === null) {
+ // If this is a plain function component without default props,
+ // and with only the default shallow comparison, we upgrade it
+ // to a SimplePureComponent to allow fast path updates.
+ workInProgress.tag = SimplePureComponent;
+ workInProgress.type = type;
+ return updateSimplePureComponent(
+ current,
+ workInProgress,
+ type,
+ nextProps,
+ updateExpirationTime,
+ renderExpirationTime,
+ );
+ }
+ let child = createFiberFromTypeAndProps(
+ Component.type,
+ null,
+ nextProps,
+ null,
+ workInProgress.mode,
+ renderExpirationTime,
+ );
+ child.ref = workInProgress.ref;
+ child.return = workInProgress;
+ workInProgress.child = child;
+ return child;
+ }
+ let currentChild = ((current.child: any): Fiber); // This is always exactly one child
if (
- current !== null &&
- (updateExpirationTime === NoWork ||
- updateExpirationTime > renderExpirationTime)
+ updateExpirationTime === NoWork ||
+ updateExpirationTime > renderExpirationTime
) {
- const prevProps = current.memoizedProps;
+ // This will be the props with resolved defaultProps,
+ // unlike current.memoizedProps which will be the unresolved ones.
+ const prevProps = currentChild.memoizedProps;
// Default to shallow comparison
let compare = Component.compare;
compare = compare !== null ? compare : shallowEqual;
- if (compare(prevProps, nextProps)) {
+ if (compare(prevProps, nextProps) && current.ref === workInProgress.ref) {
return bailoutOnAlreadyFinishedWork(
current,
workInProgress,
@@ -255,28 +288,49 @@ function updatePureComponent(
);
}
}
+ let newChild = createWorkInProgress(
+ currentChild,
+ nextProps,
+ renderExpirationTime,
+ );
+ newChild.ref = workInProgress.ref;
+ newChild.return = workInProgress;
+ workInProgress.child = newChild;
+ return newChild;
+}
- // The rest is a fork of updateFunctionComponent
- let nextChildren;
- prepareToReadContext(workInProgress, renderExpirationTime);
- if (__DEV__) {
- ReactCurrentOwner.current = workInProgress;
- ReactCurrentFiber.setCurrentPhase('render');
- nextChildren = render(nextProps);
- ReactCurrentFiber.setCurrentPhase(null);
- } else {
- nextChildren = render(nextProps);
+function updateSimplePureComponent(
+ current: Fiber | null,
+ workInProgress: Fiber,
+ Component: any,
+ nextProps: any,
+ updateExpirationTime,
+ renderExpirationTime: ExpirationTime,
+): null | Fiber {
+ if (
+ current !== null &&
+ (updateExpirationTime === NoWork ||
+ updateExpirationTime > renderExpirationTime)
+ ) {
+ const prevProps = current.memoizedProps;
+ if (
+ shallowEqual(prevProps, nextProps) &&
+ current.ref === workInProgress.ref
+ ) {
+ return bailoutOnAlreadyFinishedWork(
+ current,
+ workInProgress,
+ renderExpirationTime,
+ );
+ }
}
-
- // React DevTools reads this flag.
- workInProgress.effectTag |= PerformedWork;
- reconcileChildren(
+ return updateFunctionComponent(
current,
workInProgress,
- nextChildren,
+ Component,
+ nextProps,
renderExpirationTime,
);
- return workInProgress.child;
}
function updateFragment(
@@ -725,10 +779,7 @@ function mountLazyComponent(
let Component = readLazyComponentType(elementType);
// Store the unwrapped component in the type.
workInProgress.type = Component;
- const resolvedTag = (workInProgress.tag = resolveLazyComponentTag(
- workInProgress,
- Component,
- ));
+ const resolvedTag = (workInProgress.tag = resolveLazyComponentTag(Component));
startWorkTimer(workInProgress);
const resolvedProps = resolveDefaultProps(Component, props);
let child;
@@ -768,7 +819,7 @@ function mountLazyComponent(
null,
workInProgress,
Component,
- resolvedProps,
+ resolveDefaultProps(Component.type, resolvedProps), // The inner type can have defaults too
updateExpirationTime,
renderExpirationTime,
);
@@ -1564,10 +1615,7 @@ function beginWork(
case PureComponent: {
const type = workInProgress.type;
const unresolvedProps = workInProgress.pendingProps;
- const resolvedProps =
- workInProgress.elementType === type
- ? unresolvedProps
- : resolveDefaultProps(type, unresolvedProps);
+ const resolvedProps = resolveDefaultProps(type.type, unresolvedProps);
return updatePureComponent(
current,
workInProgress,
@@ -1577,6 +1625,16 @@ function beginWork(
renderExpirationTime,
);
}
+ case SimplePureComponent: {
+ return updateSimplePureComponent(
+ current,
+ workInProgress,
+ workInProgress.type,
+ workInProgress.pendingProps,
+ updateExpirationTime,
+ renderExpirationTime,
+ );
+ }
default:
invariant(
false,
diff --git a/packages/react-reconciler/src/ReactFiberCompleteWork.js b/packages/react-reconciler/src/ReactFiberCompleteWork.js
index 2aa17aa1e0..12069b9f91 100644
--- a/packages/react-reconciler/src/ReactFiberCompleteWork.js
+++ b/packages/react-reconciler/src/ReactFiberCompleteWork.js
@@ -35,6 +35,7 @@ import {
Profiler,
SuspenseComponent,
PureComponent,
+ SimplePureComponent,
LazyComponent,
} from 'shared/ReactWorkTags';
import {Placement, Ref, Update} from 'shared/ReactSideEffectTags';
@@ -539,6 +540,7 @@ function completeWork(
break;
case LazyComponent:
break;
+ case SimplePureComponent:
case FunctionComponent:
break;
case ClassComponent: {
diff --git a/packages/react-reconciler/src/__tests__/ReactPure-test.internal.js b/packages/react-reconciler/src/__tests__/ReactPure-test.internal.js
index a800483b42..081e15cd03 100644
--- a/packages/react-reconciler/src/__tests__/ReactPure-test.internal.js
+++ b/packages/react-reconciler/src/__tests__/ReactPure-test.internal.js
@@ -183,21 +183,49 @@ describe('pure', () => {
expect(ReactNoop.getChildren()).toEqual([span(1)]);
});
- it('warns for class components', () => {
- class SomeClass extends React.Component {
+ it('supports non-pure class components', async () => {
+ const {unstable_Suspense: Suspense} = React;
+
+ class CounterInner extends React.Component {
+ static defaultProps = {suffix: '!'};
render() {
- return null;
+ return