mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Allow arbitrary types to be wrapped in pure (#13903)
* Allow arbitrary types to be wrapped in pure This creates an outer fiber that container the pure check and an inner fiber that represents which ever type of component. * Add optimized fast path for simple pure function components Special cased when there are no defaultProps and it's a simple function component instead of class. This doesn't require an extra fiber. We could make it so that this also works with custom comparer but that means we have to go through one extra indirection to get to it. Maybe it's worth it, donno.
This commit is contained in:
1 parent
e770af7a3a
commit
15b11d23f9
10 files changed
+294
-74
No files matched your search
+26
-16
@@ -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,
|
||||
);
|
||||
|
||||
+91
-33
@@ -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,
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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 <Text text={this.props.count + '' + this.props.suffix} />;
|
||||
}
|
||||
}
|
||||
expect(() => pure(SomeClass)).toWarnDev(
|
||||
'pure: The first argument must be a function component.',
|
||||
{withoutStack: true},
|
||||
const Counter = pure(CounterInner);
|
||||
|
||||
ReactNoop.render(
|
||||
<Suspense fallback={<Text text="Loading..." />}>
|
||||
<Counter count={0} />
|
||||
</Suspense>,
|
||||
);
|
||||
expect(ReactNoop.flush()).toEqual(['Loading...']);
|
||||
await Promise.resolve();
|
||||
expect(ReactNoop.flush()).toEqual(['0!']);
|
||||
expect(ReactNoop.getChildren()).toEqual([span('0!')]);
|
||||
|
||||
// Should bail out because props have not changed
|
||||
ReactNoop.render(
|
||||
<Suspense>
|
||||
<Counter count={0} />
|
||||
</Suspense>,
|
||||
);
|
||||
expect(ReactNoop.flush()).toEqual([]);
|
||||
expect(ReactNoop.getChildren()).toEqual([span('0!')]);
|
||||
|
||||
// Should update because count prop changed
|
||||
ReactNoop.render(
|
||||
<Suspense>
|
||||
<Counter count={1} />
|
||||
</Suspense>,
|
||||
);
|
||||
expect(ReactNoop.flush()).toEqual(['1!']);
|
||||
expect(ReactNoop.getChildren()).toEqual([span('1!')]);
|
||||
});
|
||||
|
||||
it('warns if first argument is not a function', () => {
|
||||
it('warns if first argument is undefined', () => {
|
||||
expect(() => pure()).toWarnDev(
|
||||
'pure: The first argument must be a function component. Instead ' +
|
||||
'pure: The first argument must be a component. Instead ' +
|
||||
'received: undefined',
|
||||
{withoutStack: true},
|
||||
);
|
||||
|
||||
+1
-1
@@ -415,7 +415,7 @@ exports[`ReactDebugFiberPerf supports pure 1`] = `
|
||||
|
||||
⚛ (React Tree Reconciliation: Completed Root)
|
||||
⚛ Parent [mount]
|
||||
⚛ Pure(Foo) [mount]
|
||||
⚛ Foo [mount]
|
||||
|
||||
⚛ (Committing Changes)
|
||||
⚛ (Committing Snapshot Effects: 0 Total)
|
||||
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
ForwardRef,
|
||||
Profiler,
|
||||
PureComponent,
|
||||
SimplePureComponent,
|
||||
} from 'shared/ReactWorkTags';
|
||||
import invariant from 'shared/invariant';
|
||||
import ReactVersion from 'shared/ReactVersion';
|
||||
@@ -166,6 +167,7 @@ function toTree(node: ?Fiber) {
|
||||
rendered: childrenToTree(node.child),
|
||||
};
|
||||
case FunctionComponent:
|
||||
case SimplePureComponent:
|
||||
return {
|
||||
nodeType: 'component',
|
||||
type: node.type,
|
||||
|
||||
@@ -226,4 +226,131 @@ describe('forwardRef', () => {
|
||||
' in Foo (at **)',
|
||||
);
|
||||
});
|
||||
|
||||
it('should not bailout if forwardRef is not wrapped in pure', () => {
|
||||
const Component = props => <div {...props} />;
|
||||
|
||||
let renderCount = 0;
|
||||
|
||||
const RefForwardingComponent = React.forwardRef((props, ref) => {
|
||||
renderCount++;
|
||||
return <Component {...props} forwardedRef={ref} />;
|
||||
});
|
||||
|
||||
const ref = React.createRef();
|
||||
|
||||
ReactNoop.render(<RefForwardingComponent ref={ref} optional="foo" />);
|
||||
ReactNoop.flush();
|
||||
expect(renderCount).toBe(1);
|
||||
|
||||
ReactNoop.render(<RefForwardingComponent ref={ref} optional="foo" />);
|
||||
ReactNoop.flush();
|
||||
expect(renderCount).toBe(2);
|
||||
});
|
||||
|
||||
it('should bailout if forwardRef is wrapped in pure', () => {
|
||||
const Component = props => <div ref={props.forwardedRef} />;
|
||||
|
||||
let renderCount = 0;
|
||||
|
||||
const RefForwardingComponent = React.pure(
|
||||
React.forwardRef((props, ref) => {
|
||||
renderCount++;
|
||||
return <Component {...props} forwardedRef={ref} />;
|
||||
}),
|
||||
);
|
||||
|
||||
const ref = React.createRef();
|
||||
|
||||
ReactNoop.render(<RefForwardingComponent ref={ref} optional="foo" />);
|
||||
ReactNoop.flush();
|
||||
expect(renderCount).toBe(1);
|
||||
|
||||
expect(ref.current.type).toBe('div');
|
||||
|
||||
ReactNoop.render(<RefForwardingComponent ref={ref} optional="foo" />);
|
||||
ReactNoop.flush();
|
||||
expect(renderCount).toBe(1);
|
||||
|
||||
const differentRef = React.createRef();
|
||||
|
||||
ReactNoop.render(
|
||||
<RefForwardingComponent ref={differentRef} optional="foo" />,
|
||||
);
|
||||
ReactNoop.flush();
|
||||
expect(renderCount).toBe(2);
|
||||
|
||||
expect(ref.current).toBe(null);
|
||||
expect(differentRef.current.type).toBe('div');
|
||||
|
||||
ReactNoop.render(<RefForwardingComponent ref={ref} optional="bar" />);
|
||||
ReactNoop.flush();
|
||||
expect(renderCount).toBe(3);
|
||||
});
|
||||
|
||||
it('should custom pure comparisons to compose', () => {
|
||||
const Component = props => <div ref={props.forwardedRef} />;
|
||||
|
||||
let renderCount = 0;
|
||||
|
||||
const RefForwardingComponent = React.pure(
|
||||
React.forwardRef((props, ref) => {
|
||||
renderCount++;
|
||||
return <Component {...props} forwardedRef={ref} />;
|
||||
}),
|
||||
(o, p) => o.a === p.a && o.b === p.b,
|
||||
);
|
||||
|
||||
const ref = React.createRef();
|
||||
|
||||
ReactNoop.render(<RefForwardingComponent ref={ref} a="0" b="0" c="1" />);
|
||||
ReactNoop.flush();
|
||||
expect(renderCount).toBe(1);
|
||||
|
||||
expect(ref.current.type).toBe('div');
|
||||
|
||||
// Changing either a or b rerenders
|
||||
ReactNoop.render(<RefForwardingComponent ref={ref} a="0" b="1" c="1" />);
|
||||
ReactNoop.flush();
|
||||
expect(renderCount).toBe(2);
|
||||
|
||||
// Changing c doesn't rerender
|
||||
ReactNoop.render(<RefForwardingComponent ref={ref} a="0" b="1" c="2" />);
|
||||
ReactNoop.flush();
|
||||
expect(renderCount).toBe(2);
|
||||
|
||||
const ComposedPure = React.pure(
|
||||
RefForwardingComponent,
|
||||
(o, p) => o.a === p.a && o.c === p.c,
|
||||
);
|
||||
|
||||
ReactNoop.render(<ComposedPure ref={ref} a="0" b="0" c="0" />);
|
||||
ReactNoop.flush();
|
||||
expect(renderCount).toBe(3);
|
||||
|
||||
// Changing just b no longer updates
|
||||
ReactNoop.render(<ComposedPure ref={ref} a="0" b="1" c="0" />);
|
||||
ReactNoop.flush();
|
||||
expect(renderCount).toBe(3);
|
||||
|
||||
// Changing just a and c updates
|
||||
ReactNoop.render(<ComposedPure ref={ref} a="2" b="2" c="2" />);
|
||||
ReactNoop.flush();
|
||||
expect(renderCount).toBe(4);
|
||||
|
||||
// Changing just c does not update
|
||||
ReactNoop.render(<ComposedPure ref={ref} a="2" b="2" c="3" />);
|
||||
ReactNoop.flush();
|
||||
expect(renderCount).toBe(4);
|
||||
|
||||
// Changing ref still rerenders
|
||||
const differentRef = React.createRef();
|
||||
|
||||
ReactNoop.render(<ComposedPure ref={differentRef} a="2" b="2" c="3" />);
|
||||
ReactNoop.flush();
|
||||
expect(renderCount).toBe(5);
|
||||
|
||||
expect(ref.current).toBe(null);
|
||||
expect(differentRef.current.type).toBe('div');
|
||||
});
|
||||
});
|
||||
@@ -7,34 +7,26 @@
|
||||
|
||||
import {REACT_PURE_TYPE} from 'shared/ReactSymbols';
|
||||
|
||||
import isValidElementType from 'shared/isValidElementType';
|
||||
import warningWithoutStack from 'shared/warningWithoutStack';
|
||||
|
||||
export default function pure<Props>(
|
||||
render: (props: Props) => React$Node,
|
||||
type: React$ElementType,
|
||||
compare?: (oldProps: Props, newProps: Props) => boolean,
|
||||
) {
|
||||
if (__DEV__) {
|
||||
if (typeof render !== 'function') {
|
||||
if (!isValidElementType(type)) {
|
||||
warningWithoutStack(
|
||||
false,
|
||||
'pure: The first argument must be a function component. Instead ' +
|
||||
'pure: The first argument must be a component. Instead ' +
|
||||
'received: %s',
|
||||
render === null ? 'null' : typeof render,
|
||||
type === null ? 'null' : typeof type,
|
||||
);
|
||||
} else {
|
||||
const prototype = render.prototype;
|
||||
if (prototype && prototype.isReactComponent) {
|
||||
warningWithoutStack(
|
||||
false,
|
||||
'pure: The first argument must be a function component. Classes ' +
|
||||
'are not supported. Use React.PureComponent instead.',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
$$typeof: REACT_PURE_TYPE,
|
||||
render,
|
||||
type,
|
||||
compare: compare === undefined ? null : compare,
|
||||
};
|
||||
}
|
||||
@@ -43,4 +43,5 @@ export const ForwardRef = 11;
|
||||
export const Profiler = 12;
|
||||
export const SuspenseComponent = 13;
|
||||
export const PureComponent = 14;
|
||||
export const LazyComponent = 15;
|
||||
export const SimplePureComponent = 15;
|
||||
export const LazyComponent = 16;
|
||||
@@ -80,7 +80,7 @@ function getComponentName(type: mixed): string | null {
|
||||
case REACT_FORWARD_REF_TYPE:
|
||||
return getWrappedName(type, type.render, 'ForwardRef');
|
||||
case REACT_PURE_TYPE:
|
||||
return getWrappedName(type, type.render, 'Pure');
|
||||
return getComponentName(type.type);
|
||||
case REACT_LAZY_TYPE: {
|
||||
const thenable: LazyComponent<mixed> = (type: any);
|
||||
const resolvedThenable = refineResolvedLazyComponent(thenable);
|
||||
|
||||
Reference in new issue
Block a user