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

Block Editor: Add hook: useNoRecursiveRenders #28428

Merged
merged 4 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export { default as WritingFlow } from './writing-flow';
export { useCanvasClickRedirect as __unstableUseCanvasClickRedirect } from './use-canvas-click-redirect';
export { default as useBlockDisplayInformation } from './use-block-display-information';
export { default as __unstableIframe } from './iframe';
export { default as __experimentalUseNoRecursiveRenders } from './use-no-recursive-renders';

/*
* State Related Components
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* WordPress dependencies
*/
import {
createContext,
useCallback,
useContext,
useMemo,
} from '@wordpress/element';

const RenderedRefsContext = createContext( new Set() );

// Immutably add to a Set
function add( set, element ) {
const result = new Set( set );
result.add( element );
return result;
}

export default function useNoRecursiveRenders( uniqueId ) {
Copy link
Member

Choose a reason for hiding this comment

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

JSDoc comment with an example should help create README.md once it gets promoted stable. In addition, it will make it easier to type the package with TS in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How's this? 1c3b036

I'm not familiar with our latest typing practice. Do you have a specific React type you recommend for the returned provider? For now I just used Function.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this is exactly what I meant 👍🏻

If you think it deserves an example, you can use the following syntax:

* @example
* <caption>ESNext</caption>
* ```js
* // Using ESNext syntax
* import { PluginArea } from '@wordpress/plugins';
*
* const Layout = () => (
* <div>
* Content of the page
* <PluginArea />
* </div>
* );
* ```

const previouslyRenderedBlocks = useContext( RenderedRefsContext );
const hasAlreadyRendered = previouslyRenderedBlocks.has( uniqueId );
const newRenderedBlocks = useMemo(
() => add( previouslyRenderedBlocks, uniqueId ),
[ uniqueId, previouslyRenderedBlocks ]
);
const Provider = useCallback(
( { children } ) => (
<RenderedRefsContext.Provider value={ newRenderedBlocks }>
{ children }
</RenderedRefsContext.Provider>
),
[ newRenderedBlocks ]
);
return [ hasAlreadyRendered, Provider ];
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { render } from '@testing-library/react';
* WordPress dependencies
*/
import { Fragment } from '@wordpress/element';
import { __experimentalUseNoRecursiveRenders as useNoRecursiveRenders } from '@wordpress/block-editor';

// Mimics a block's Edit component, such as ReusableBlockEdit, which is capable
// of calling itself depending on its `ref` attribute.
Expand Down Expand Up @@ -37,11 +38,6 @@ function Edit( { attributes: { ref } } ) {
);
}

/**
* Internal dependencies
*/
import useNoRecursiveRenders from '../use-no-recursive-renders';

describe( 'useNoRecursiveRenders', () => {
it( 'allows a single block to render', () => {
const { container } = render(
Expand Down
6 changes: 1 addition & 5 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { __ } from '@wordpress/i18n';
import {
__experimentalUseInnerBlocksProps as useInnerBlocksProps,
__experimentalUseNoRecursiveRenders as useNoRecursiveRenders,
InnerBlocks,
BlockControls,
InspectorControls,
Expand All @@ -26,11 +27,6 @@ import {
} from '@wordpress/block-editor';
import { store as reusableBlocksStore } from '@wordpress/reusable-blocks';

/**
* Internal dependencies
*/
import useNoRecursiveRenders from './use-no-recursive-renders';

export default function ReusableBlockEdit( { attributes: { ref }, clientId } ) {
const [ hasAlreadyRendered, RecursionProvider ] = useNoRecursiveRenders(
ref
Expand Down
29 changes: 0 additions & 29 deletions packages/block-library/src/block/use-no-recursive-renders.js

This file was deleted.