Skip to content

Commit

Permalink
Implement forward ref and memo
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Apr 14, 2021
1 parent efc0084 commit f7d5c48
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions packages/react-server/src/ReactFizzServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,22 +728,47 @@ function validateFunctionComponentInDev(Component: any): void {
}
}

function resolveDefaultProps(Component: any, baseProps: Object): Object {
if (Component && Component.defaultProps) {
// Resolve default props. Taken from ReactElement
const props = Object.assign({}, baseProps);
const defaultProps = Component.defaultProps;
for (const propName in defaultProps) {
if (props[propName] === undefined) {
props[propName] = defaultProps[propName];
}
}
return props;
}
return baseProps;
}

function renderForwardRef(
request: Request,
task: Task,
type: any,
props: Object,
ref: any,
): void {
throw new Error('Not yet implemented element type.');
renderWithHooks(request, task, type, props, ref);
}

function renderMemo(
request: Request,
task: Task,
type: any,
props: Object,
ref: any,
): void {
throw new Error('Not yet implemented element type.');
const innerType = type.type;
const resolvedProps = resolveDefaultProps(innerType, props);
renderElement(
request,
task,
innerType,
resolvedProps,
ref,
);
}

function renderContextConsumer(
Expand Down Expand Up @@ -835,6 +860,7 @@ function renderElement(
task: Task,
type: any,
props: Object,
ref: any,
): void {
if (typeof type === 'function') {
if (shouldConstruct(type)) {
Expand Down Expand Up @@ -877,11 +903,11 @@ function renderElement(
if (typeof type === 'object' && type !== null) {
switch (type.$$typeof) {
case REACT_FORWARD_REF_TYPE: {
renderForwardRef(request, task, type, props);
renderForwardRef(request, task, type, props, ref);
return;
}
case REACT_MEMO_TYPE: {
renderMemo(request, task, type, props);
renderMemo(request, task, type, props, ref);
return;
}
case REACT_PROVIDER_TYPE: {
Expand Down Expand Up @@ -961,7 +987,8 @@ function renderNodeDestructive(
const element: React$Element<any> = (node: any);
const type = element.type;
const props = element.props;
renderElement(request, task, type, props);
const ref = element.ref;
renderElement(request, task, type, props, ref);
return;
}
case REACT_PORTAL_TYPE:
Expand Down

0 comments on commit f7d5c48

Please sign in to comment.