Maintains and determines the current state of the active tab.

useActiveTab(initialTabName: string, tabs: Array): Array
Since: 1.0.0
Parameters
initialTabName (string) Initial tab element to be selected upon mounting of component.
tabs (Array) Tabs stored from the previous state.
Returns
Array: Returns a stateful value, and a function to update it.
Example
const [ activeTab, setActiveTab ] = useActiveTab( initialTabName, tabs );

Conditionally return an instance of useRef.

useConditionalRef(isSave: boolean, initialValue: any): Object
Since: 1.0.0
Parameters
isSave (boolean) Whether the field is meant to be rendered on the front-end.
initialValue (any) Initial value used during the initial render.
Returns
Object: Returns a mutable ref object whose .current property is initialized to the passed argument.
Example
const anchorRef = useConditionalRef( isSave );

This hook mimicks the componentDidMount hook for React.

useDidMount(callback: Function): void
Since: 1.0.0
Parameters
callback (Function) Function to be called when component is mounted.
Returns
void:
Example
useDidMount( () => {
    console.log( 'Mounted!' );
} );

This hook mimicks the componentDidUpdate hook for React. Fires a callback on component update, and can take in a list of conditions to fire callback when one of the conditions changes.

useDidUpdate(callback: Function, conditions: Array): void
Since: 1.0.0
Parameters
callback (Function) Function to be called when component is updated.
conditions (Array) The list of variables which trigger update when they are changed.
Returns
void:
Example
useDidUpdate( () => {
    console.log( 'Updated!' );
}, [ var1, var2 ] );

Finds the selected post object based on the given post id and maintains the state of change when it happens.

useFindPostById(postId: number, postsQuery: Array, needle: string): (null | Object)
Since: 1.0.0
Parameters
postId (number) Selected post id.
postsQuery (Array) List of posts retrieved from the API query.
needle (string) The searched key/id.
Returns
(null | Object): Post object.
Example
const post = useFindPostById( postId, postsQuery );

Retrieves the author of the current post.

useGetAuthorData(dependencies: (null | Array)): Object
Since: 1.2.0
Parameters
dependencies ((null | Array)) An array of optional dependencies to refresh the hook output.
Returns
Object: Returns u data and title within an object.
Example
const { authorId, authorEmail } = useGetAuthorData();

Retrieves the block support value for the "Layout" settings to be used in the preview.

useGetBlockLayout(blockTypeOrName: string): Object
Since: 1.0.0
Parameters
blockTypeOrName (string) The name for a block is a unique string that identifies a block.
Returns
Object: Block level "Layout" settings object.
Example
const { default } = useGetBlockLayout( 'core/social-links' );

// { 'type': 'flex' }

Returns an object containing the given registered block meta-data.

useGetBlockType(clientId: string): Object
Since: 1.5.0
Parameters
clientId (string) The block’s client id.
Returns
Object: Returns the given block meta-data.
Example
const currentBlock = useGetBlockType( '74b85937-4f07-8cafb54f16d4' );

Returns an object containing block variations info. The returned object also determines which variation is currently active, and when there are multiple variations annotated as the default one, the last added item is picked.

useGetBlockVariations(clientId: string): Object
Since: 1.2.0
Parameters
clientId (string) The block’s client id.
Returns
Object: Returns variations info of a given block id.
Example
const { defaultVariation, variations } = useGetBlockVariations( '74b85937-4f07-8cafb54f16d4' );

Returns the post currently being edited in its last known saved state, not including unsaved edits. Returns an object containing relevant default post values if the post has not yet been saved.

useGetCurrentPost(): Object
Since: 1.2.0
Returns
Object: Returns the post data currently being edited.
Example
const { postId, postTitle } = useGetCurrentPost();

Returns an object containing relevant editor settings.

useGetEditorSettings(): Object
Since: 1.6.0
Returns
Object: Returns editor settings object.
Example
const { colors, fontSizes } = useGetEditorSettings();

Retrieve media MIME type based on given attachment id.

useGetMediaType(id: number): string
Since: 1.0.0
Parameters
id (number) Attachment ID.
Returns
string: Retrieve media type associated with the given attachment ID known as MIME type.
Example
const backgroundType = useGetMediaType( id );

This hook allows for generating custom (notification) toast messages.

useToast(): Function
Since: 1.0.0
Returns
Function: A function that could be called to initiate toast messages.
Example
const toast = useToast();
toast( 'Text to display as a toast message!' );

Retrieve list of HTML nodes genearted for each post item.

useGetNodeList(endpoint: string, args: Object, predicate: Function): Array
Since: 1.0.0
Parameters
endpoint (string) API node endpoint.
args (Object) Arguments to be passed to the apiFetch method.
predicate (Function) The function invoked per property. The predicate is invoked with two arguments: (value, key).
Returns
Array: List of HTML nodes to be referred to when each post item looped over.
Example
const nodeList = useGetNodeList( 'namespace-recent-posts/v1/nodes' );

Retrieves the current post object given the post ID and post-type.

useGetPost(postId: number, postType: string): Object
Since: 1.0.0
Parameters
postId (number) Post ID.
postType (string) Post-type name (key).
Returns
Object: Returns the post object.
Example
const post = useGetPost( 748, 'page' );

Retrieves a post meta field of the current post.

useGetPostMeta(metaKey: string): Object
Since: 1.2.0
Parameters
metaKey (string) The meta key to retrieve.
Returns
Object: An object containing currently viewed post-id and meta field value.
Example
const { metaValue, postId, postType } = useGetPostMeta( 'meta_key' );

Retrieve a list of post-type posts and refresh the list when any direct arguments change.

useGetPosts(args: Object, clientId: string, postType: string): Object
Since: 1.0.0
Parameters
args (Object) Arguments to be passed to the apiFetch method.
clientId (string) The block’s client id.
postType (string) Post type name.
Returns
Object: List of posts retrieved from the API along with a list of options to select from.
Example
const { postsOptions, postsQuery } = useGetPosts( { order: 'asc' }, clientId, 'posts' );

Retrieve a list of product posts and refresh the list when any direct arguments change.

useGetProducts(args: Object, clientId: string): Object
Since: 1.0.0
Parameters
args (Object) Arguments to be passed to the apiFetch method.
clientId (string) The block’s client id.
Returns
Object: List of posts retrieved from the API along with a list of options to select from.
Example
const { productsOptions, productsQuery } = useGetProducts( { order: 'asc' }, clientId );

Retrieve a list of product taxonomy terms and refresh the list when any direct arguments change.

useGetProductTerms(taxonomy: string, args: Object): Object
Since: 1.0.0
Parameters
taxonomy (string) Taxonomy name.
args (Object) Arguments to be passed to the apiFetch method.
Returns
Object: List of terms retrieved from the API along with a list of options to select from.
Example
const { termsOptions, termsQuery } = useGetProductTerms( 'categories' );

Retrieves the current site data or object.

useGetSiteData(dependencies: (null | Array)): Object
Since: 1.0.0
Parameters
dependencies ((null | Array)) An array of optional dependencies to refresh the hook output.
Returns
Object: Returns site data and title within an object.
Example
const { siteTitle } = useGetSiteData();

Retrieve a list of taxonomy terms and refresh the list when any direct arguments change.

useGetTerms(taxonomy: string, args: Object): Object
Since: 1.0.0
Parameters
taxonomy (string) Taxonomy name.
args (Object) Arguments to be passed to the apiFetch method.
Returns
Object: List of terms retrieved from the API along with a list of options to select from.
Example
const { termsOptions, termsQuery } = useGetTerms( 'categories' );

Determines whether a block has already inner blocks inserted.

useHasInnerBlocks(clientId: string): boolean
Since: 1.2.0
Parameters
clientId (string) The block’s client id.
Returns
boolean: Whether a block has inner blocks inserted.
Example
const hasInnerBlocks = useHasInnerBlocks( '74b85937-4f07-8cafb54f16d4' );

Detect whether the mouse is hovering an element.

useHover(): Object
Since: 1.3.0
Returns
Object: A ref and a boolean value indicating whether the element with that ref is currently being hovered.
Example
const [ hoverRef, isHovered ] = useHover();

return <div ref={hoverRef}>{ isHovered ? "😁" : "☹️" }</div>;

This module merely adds backward compatibility for the usage of experimental useInnerBlocksProps in projects created using WordPress versions prior to 5.9.

useInnerBlocksProps
Since: 1.0.0
Example
const blockProps = useBlockProps( { className: 'my-class' } );
const innerBlocksProps = useInnerBlocksProps(
    blockProps,
    { allowedBlocks: [ 'core/heading', 'core/paragraph', 'core/image' ] }
);

This hook takes an initial value for an input field and updates it when the onChange event raises.

useInputValue(initialValue: string): Array
Since: 1.0.0
Parameters
initialValue (string) Initial value of the field.
Returns
Array: Returns a stateful value, and a function to update it.
Example
const [ value, setValue ] = useInputValue( 'Hello' );
<TextControl onChange={ setValue } value={ value } />

This hook determines whether an element is being dragged within another (ref) target element.

useIsDraggingWithin(elementRef: Object): boolean
Since: 1.0.0
Parameters
elementRef (Object) A mutable ref element object whose .current property is initialized to the passed argument.
Returns
boolean: Is dragging within the target element.
Example
const listItemRef = useRef( null );
const isDraggingWithin = useIsDraggingWithin( listItemRef );

Determines whether the block corresponding to the specified client-ID is currently selected with consideration to multi-selection within its parent block.

useIsParentSelected(clientId: string): boolean
Since: 1.4.0
Parameters
clientId (string) The block’s client id.
Returns
boolean: Whether parent or direct child block is selected.
Example
const isParentSelected = useIsParentSelected( clientId );

Generates latitude & longitude from a given address.

useLatLngBounds(address: string, apiKey: string, locale: string): (Object | string)
Since: 1.0.0
Parameters
address (string) Address.
apiKey (string) GoogleMaps API key.
locale (string) Region and language code of the parsed address.
Returns
(Object | string): latitude & longitude object or error message.
Example
const latLng = useLatLngBounds( 'Skyland Istanbul, Sarıyer, Turkey', 'H7ZH7p3dHa24...', 'en );

This hook maintains the state of post-type name and REST-API base key extraction from the given string value.

usePostTypeNameRestBase(postType: string): Object
Since: 1.0.0
Parameters
postType (string) Post type name and rest-base key.
Returns
Object: Object of post-type name and REST-API base key.
Example
const { postTypeKey, restBaseKey } = usePostTypeNameRestBase( 'post|posts' );

Determines whether the current WordPress query has posts to loop over, and slices the query according to the maximum limit determined.

usePreparePosts(ids: Array, limit: number, query: Array): Object
Since: 1.2.2
Parameters
ids (Array) Handpicked post ids.
limit (number) Maximum number of posts to show.
query (Array) List of posts retrieved from the API query.
Returns
Object: Sliced query, maximum number of available posts, and whether there are posts to loop over.
Example
const { havePosts, maxLimit, slicedQuery } = usePreparePosts( [2], 3, [ { id: 1, title: 'Post A' }, { id: 2, title: 'Post B' } ] );

A setTimeout hook that calls a callback after a timeout duration.

useTimeout(callback: Function, timeoutDelayMs: number): Object
Since: 1.0.0
Parameters
callback (Function) The callback to be invoked after timeout.
timeoutDelayMs (number) Amount of time in ms after which to invoke.
Returns
Object: Current state of the timer and methods to start and stop the timer.
Example
const [ isBusy, toggleIsBusy ] = useToggle( true );
const { start } = useTimeout( toggleIsBusy, 2000 );
start();

This hook takes a parameter with value and allows for quickly toggling the value.

useToggle(initialValue: (any | boolean), toggleFunction: Function): Array
Since: 1.0.0
Parameters
initialValue ((any | boolean)) Initial value of the toggle.
toggleFunction (Function) A toggle function. This allows for non boolean toggles.
Returns
Array: Returns a stateful value, and a function to update it.
Example
const [ value1, toggleValue1 ] = useToggle();
const [ value2, toggleValue2 ] = useToggle( true );
const [ value3, toggleValue3 ] = useToggle( 'start', customToggleFunction );

Updates a post meta field based on the given post ID.

useUpdatePostMeta(metaKey: string, metaValue: string, postId: number, postType: string): Function
Since: 1.0.0
Parameters
metaKey (string) The meta key to retrieve.
metaValue (string) Metadata value.
postId (number) Post ID.
postType (string) Post type key/name.
Returns
Function: A callback function invoked when the component requires update post-meta data.
Example
const setMeta = useUpdatePostMeta( 'prefix_theme_meta', { title: false }, 8, 'post' );

Get the current size of the browser window.

useWindowSize(): Object
Since: 1.3.0
Returns
Object: An object containing the window's width and height.
Example
const size = useWindowSize();