Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC #30: React.forwardRef implementation #12346

Merged
merged 22 commits into from
Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
6 changes: 6 additions & 0 deletions packages/react-is/src/ReactIs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
REACT_PORTAL_TYPE,
REACT_PROVIDER_TYPE,
REACT_STRICT_MODE_TYPE,
REACT_FORWARD_REF_TYPE,
} from 'shared/ReactSymbols';

export function typeOf(object: any) {
Expand All @@ -38,6 +39,7 @@ export function typeOf(object: any) {
switch ($$typeofType) {
case REACT_CONTEXT_TYPE:
case REACT_PROVIDER_TYPE:
case REACT_FORWARD_REF_TYPE:
return $$typeofType;
default:
return $$typeof;
Expand All @@ -58,6 +60,7 @@ export const Element = REACT_ELEMENT_TYPE;
export const Fragment = REACT_FRAGMENT_TYPE;
export const Portal = REACT_PORTAL_TYPE;
export const StrictMode = REACT_STRICT_MODE_TYPE;
export const ForwardRef = REACT_FORWARD_REF_TYPE;

export function isAsyncMode(object: any) {
return typeOf(object) === REACT_ASYNC_MODE_TYPE;
Expand All @@ -84,3 +87,6 @@ export function isPortal(object: any) {
export function isStrictMode(object: any) {
return typeOf(object) === REACT_STRICT_MODE_TYPE;
}
export function isForwardRef(object: any) {
return typeOf(object) === REACT_FORWARD_REF_TYPE;
}
9 changes: 9 additions & 0 deletions packages/react-is/src/__tests__/ReactIs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,13 @@ describe('ReactIs', () => {
expect(ReactIs.isStrictMode(<React.unstable_AsyncMode />)).toBe(false);
expect(ReactIs.isStrictMode(<div />)).toBe(false);
});

it('should identify ref forwarding component', () => {
const RefForwardingComponent = React.forwardRef((props, ref) => null);
expect(ReactIs.typeOf(<RefForwardingComponent />)).toBe(ReactIs.ForwardRef);
expect(ReactIs.isForwardRef(<RefForwardingComponent />)).toBe(true);
expect(ReactIs.isForwardRef({type: ReactIs.StrictMode})).toBe(false);
expect(ReactIs.isForwardRef(<React.unstable_AsyncMode />)).toBe(false);
expect(ReactIs.isForwardRef(<div />)).toBe(false);
});
});
65 changes: 63 additions & 2 deletions packages/react-reconciler/src/ReactChildFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
REACT_ELEMENT_TYPE,
REACT_FRAGMENT_TYPE,
REACT_PORTAL_TYPE,
REACT_FORWARD_REF_TYPE,
} from 'shared/ReactSymbols';
import {
FunctionalComponent,
Expand Down Expand Up @@ -466,6 +467,26 @@ function ChildReconciler(shouldTrackSideEffects) {
}

if (typeof newChild === 'object' && newChild !== null) {
if (
typeof newChild.type === 'object' &&
newChild.type !== null &&
newChild.type.$$typeof === REACT_FORWARD_REF_TYPE
) {
const renderFn = newChild.type.renderFn;
invariant(
typeof renderFn === 'function',
'forwardRef requires a render function but was given %s.%s',
renderFn === null ? 'null' : typeof renderFn,
ReactDebugCurrentFiber.getCurrentFiberStackAddendum() || '',
);

return createChild(
returnFiber,
renderFn(newChild.props, newChild.ref),
expirationTime,
);
}

switch (newChild.$$typeof) {
case REACT_ELEMENT_TYPE: {
const created = createFiberFromElement(
Expand Down Expand Up @@ -1128,6 +1149,32 @@ function ChildReconciler(shouldTrackSideEffects) {
child = child.sibling;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The element.type won't line up on line 1125 which means that it'll kill the state of the subtree every time this rerenders.

}

if (
typeof element.type === 'object' &&
element.type !== null &&
element.type.$$typeof === REACT_FORWARD_REF_TYPE
) {
const renderFn = element.type.renderFn;
invariant(
typeof renderFn === 'function',
'forwardRef requires a render function but was given %s.%s',
renderFn === null ? 'null' : typeof renderFn,
ReactDebugCurrentFiber.getCurrentFiberStackAddendum() || '',
);

const newElement = renderFn(element.props, element.ref);
if (newElement == null) {
return null;
}

return reconcileSingleElement(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've tried pretty hard to avoid recursive algorithms so that we could try to inline these into hot loops instead. Would prefer a while loop.

returnFiber,
currentFirstChild,
newElement,
expirationTime,
);
}

if (element.type === REACT_FRAGMENT_TYPE) {
const created = createFiberFromFragment(
element.props.children,
Expand Down Expand Up @@ -1213,10 +1260,24 @@ function ChildReconciler(shouldTrackSideEffects) {
if (
typeof newChild === 'object' &&
newChild !== null &&
newChild.type === REACT_FRAGMENT_TYPE &&
newChild.key === null
) {
newChild = newChild.props.children;
if (newChild.type === REACT_FRAGMENT_TYPE) {
newChild = newChild.props.children;
} else if (
typeof newChild.type === 'object' &&
newChild.type !== null &&
newChild.type.$$typeof === REACT_FORWARD_REF_TYPE
) {
const renderFn = newChild.type.renderFn;
invariant(
typeof renderFn === 'function',
'forwardRef requires a render function but was given %s.%s',
renderFn === null ? 'null' : typeof renderFn,
ReactDebugCurrentFiber.getCurrentFiberStackAddendum() || '',
);
newChild = renderFn(newChild.props, newChild.ref);
}
}

// Handle object types
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
isValidElement,
} from './ReactElement';
import {createContext} from './ReactContext';
import forwardRef from './forwardRef';
import {
createElementWithValidation,
createFactoryWithValidation,
Expand All @@ -45,6 +46,7 @@ const React = {
PureComponent,

createContext,
forwardRef,

Fragment: REACT_FRAGMENT_TYPE,
StrictMode: REACT_STRICT_MODE_TYPE,
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/ReactElementValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
REACT_ASYNC_MODE_TYPE,
REACT_PROVIDER_TYPE,
REACT_CONTEXT_TYPE,
REACT_FORWARD_REF_TYPE,
} from 'shared/ReactSymbols';
import checkPropTypes from 'prop-types/checkPropTypes';
import warning from 'fbjs/lib/warning';
Expand Down Expand Up @@ -297,7 +298,8 @@ export function createElementWithValidation(type, props, children) {
(typeof type === 'object' &&
type !== null &&
(type.$$typeof === REACT_PROVIDER_TYPE ||
type.$$typeof === REACT_CONTEXT_TYPE));
type.$$typeof === REACT_CONTEXT_TYPE ||
type.$$typeof === REACT_FORWARD_REF_TYPE));

// We warn in this case but don't throw. We expect the element creation to
// succeed and there will likely be errors in render.
Expand Down
Loading