1.6.0
Maintains and determines the current state of the active tab.
(string)
Initial tab element to be selected upon mounting of component.
(Array)
Tabs stored from the previous state.
Array
:
Returns a stateful value, and a function to update it.
const [ activeTab, setActiveTab ] = useActiveTab( initialTabName, tabs );
Conditionally return an instance of useRef
.
(boolean)
Whether the field is meant to be rendered on the front-end.
(any)
Initial value used during the initial render.
Object
:
Returns a mutable ref object whose
.current
property is initialized to the passed argument.
const anchorRef = useConditionalRef( isSave );
This hook mimicks the componentDidMount
hook for React.
(Function)
Function to be called when component is mounted.
void
:
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.
(Function)
Function to be called when component is updated.
(Array)
The list of variables which trigger update when they are changed.
void
:
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.
(number)
Selected post id.
(Array)
List of posts retrieved from the API query.
(string)
The searched key/id.
(null | Object)
:
Post object.
const post = useFindPostById( postId, postsQuery );
Retrieves the author of the current post.
((null | Array))
An array of optional dependencies to refresh the hook output.
Object
:
Returns u data and title within an object.
const { authorId, authorEmail } = useGetAuthorData();
Retrieves the block support value for the "Layout" settings to be used in the preview.
(string)
The name for a block is a unique string that identifies a block.
Object
:
Block level "Layout" settings object.
const { default } = useGetBlockLayout( 'core/social-links' );
// { 'type': 'flex' }
Returns an object containing the given registered block meta-data.
(string)
The block’s client id.
Object
:
Returns the given block meta-data.
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.
(string)
The block’s client id.
Object
:
Returns variations info of a given block id.
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.
Object
:
Returns the post data currently being edited.
const { postId, postTitle } = useGetCurrentPost();
Returns an object containing relevant editor settings.
Object
:
Returns editor settings object.
const { colors, fontSizes } = useGetEditorSettings();
Retrieve media MIME type based on given attachment id.
(number)
Attachment ID.
string
:
Retrieve media type associated with the given attachment ID known as MIME type.
const backgroundType = useGetMediaType( id );
This hook allows for generating custom (notification) toast messages.
Function
:
A function that could be called to initiate toast messages.
const toast = useToast();
toast( 'Text to display as a toast message!' );
Retrieve list of HTML nodes genearted for each post item.
(string)
API node endpoint.
(Object)
Arguments to be passed to the apiFetch method.
(Function)
The function invoked per property. The predicate is invoked with two arguments: (value, key).
Array
:
List of HTML nodes to be referred to when each post item looped over.
const nodeList = useGetNodeList( 'namespace-recent-posts/v1/nodes' );
Retrieves the current post object given the post ID and post-type.
Object
:
Returns the post object.
const post = useGetPost( 748, 'page' );
Retrieves a post meta field of the current post.
(string)
The meta key to retrieve.
Object
:
An object containing currently viewed post-id and meta field value.
const { metaValue, postId, postType } = useGetPostMeta( 'meta_key' );
Retrieve a list of post-type posts and refresh the list when any direct arguments change.
(Object)
Arguments to be passed to the apiFetch method.
(string)
The block’s client id.
(string)
Post type name.
Object
:
List of posts retrieved from the API along with a list of options to select from.
const { postsOptions, postsQuery } = useGetPosts( { order: 'asc' }, clientId, 'posts' );
Retrieve a list of product posts and refresh the list when any direct arguments change.
(Object)
Arguments to be passed to the apiFetch method.
(string)
The block’s client id.
Object
:
List of posts retrieved from the API along with a list of options to select from.
const { productsOptions, productsQuery } = useGetProducts( { order: 'asc' }, clientId );
Retrieve a list of product taxonomy terms and refresh the list when any direct arguments change.
Object
:
List of terms retrieved from the API along with a list of options to select from.
const { termsOptions, termsQuery } = useGetProductTerms( 'categories' );
Retrieves the current site data or object.
((null | Array))
An array of optional dependencies to refresh the hook output.
Object
:
Returns site data and title within an object.
const { siteTitle } = useGetSiteData();
Retrieve a list of taxonomy terms and refresh the list when any direct arguments change.
Object
:
List of terms retrieved from the API along with a list of options to select from.
const { termsOptions, termsQuery } = useGetTerms( 'categories' );
Determines whether a block has already inner blocks inserted.
(string)
The block’s client id.
boolean
:
Whether a block has inner blocks inserted.
const hasInnerBlocks = useHasInnerBlocks( '74b85937-4f07-8cafb54f16d4' );
Detect whether the mouse is hovering an element.
Object
:
A ref and a boolean value indicating whether the element with that ref is currently being hovered.
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.
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.
(string)
Initial value of the field.
Array
:
Returns a stateful value, and a function to update it.
const [ value, setValue ] = useInputValue( 'Hello' );
<TextControl onChange={ setValue } value={ value } />
This hook determines whether an element is being dragged within another (ref) target element.
(Object)
A mutable ref element object whose
.current
property is initialized to the passed argument.
boolean
:
Is dragging within the target element.
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.
(string)
The block’s client id.
boolean
:
Whether parent or direct child block is selected.
const isParentSelected = useIsParentSelected( clientId );
Generates latitude & longitude from a given address.
(string)
Address.
(string)
GoogleMaps API key.
(string)
Region and language code of the parsed address.
(Object | string)
:
latitude & longitude object or error message.
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.
(string)
Post type name and rest-base key.
Object
:
Object of post-type name and REST-API base key.
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.
(Array)
Handpicked post ids.
(number)
Maximum number of posts to show.
(Array)
List of posts retrieved from the API query.
Object
:
Sliced query, maximum number of available posts, and whether there are posts to loop over.
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.
(Function)
The callback to be invoked after timeout.
(number)
Amount of time in ms after which to invoke.
Object
:
Current state of the timer and methods to start and stop the timer.
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.
((any | boolean))
Initial value of the toggle.
(Function)
A toggle function. This allows for non boolean toggles.
Array
:
Returns a stateful value, and a function to update it.
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.
(string)
The meta key to retrieve.
(string)
Metadata value.
(number)
Post ID.
(string)
Post type key/name.
Function
:
A callback function invoked when the component requires update post-meta data.
const setMeta = useUpdatePostMeta( 'prefix_theme_meta', { title: false }, 8, 'post' );
Get the current size of the browser window.
Object
:
An object containing the window's width and height.
const size = useWindowSize();