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
4 changes: 4 additions & 0 deletions packages/element/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fixes

- `serialize`/`renderToString`: Render `memo`-wrapped components instead of serializing them to an empty string ([#81986](https://github.com/WordPress/gutenberg/pull/81986)).

### Internal

- Split tsconfig into a build project and a default dev project so dev files are type checked without publishing their declarations. ([#81514](https://github.com/WordPress/gutenberg/pull/81514))
Expand Down
22 changes: 21 additions & 1 deletion packages/element/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ import {
escapeAttribute,
isValidAttributeName,
} from '@wordpress/escape-html';
import { createContext, Fragment, StrictMode, forwardRef } from './react';
import {
createContext,
createElement,
Fragment,
StrictMode,
forwardRef,
memo,
} from './react';
import RawHTML from './raw-html';

/** @typedef {React.ReactElement} ReactElement */
Expand Down Expand Up @@ -71,6 +78,10 @@ const ForwardRef = forwardRef( () => {
return null;
} );

const Memo = memo( () => {
return null;
} );

/**
* Valid attribute types.
*/
Expand Down Expand Up @@ -625,6 +636,15 @@ export function renderElement(
context,
legacyContext
);

case Memo.$$typeof:
// Memoization is meaningless for a static, one-off serialization,
// so the wrapped type is rendered directly.
return renderElement(
createElement( type.type, props ),
context,
legacyContext
);
}

return '';
Expand Down
23 changes: 23 additions & 0 deletions packages/element/src/test/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Fragment,
StrictMode,
forwardRef,
memo,
} from '../react';
import RawHTML from '../raw-html';
import serialize, {
Expand Down Expand Up @@ -86,6 +87,28 @@ describe( 'serialize()', () => {
expect( result ).toBe( '<div>test</div>' );
} );

it( 'should render with memo', () => {
const MemoizedComponent = memo( ( { value } ) => {
return <div>{ value }</div>;
} );

const result = serialize( <MemoizedComponent value="test" /> );

expect( result ).toBe( '<div>test</div>' );
} );

it( 'should render with memo wrapping forwardRef', () => {
const MemoizedComponent = memo(
forwardRef( () => {
return <div>test</div>;
} )
);

const result = serialize( <MemoizedComponent /> );

expect( result ).toBe( '<div>test</div>' );
} );

describe( 'empty attributes', () => {
it( 'should not render a null attribute value', () => {
const result = serialize( <video src={ undefined } /> );
Expand Down
Loading