Title: Application state managed withDispatch, withSelect and compose 101
Author: Dean Sas
Published: December 13, 2022
Last modified: March 13, 2023

---

 * [Showcase](https://wordpress.org/showcase/)
 * [Plugins](https://wordpress.org/plugins/)
 * [Themes](https://wordpress.org/themes/)
 * [Hosting](https://wordpress.org/hosting/)
 * [News](https://wordpress.org/news/)
    - [Learn WordPress](https://learn.wordpress.org/)
    - [Documentation](https://wordpress.org/documentation/)
    - [Education](https://wordpress.org/education/)
    - [Forums](https://wordpress.org/support/forums/)
    - [Developers](https://developer.wordpress.org/)
    - [Blocks](https://wordpress.org/blocks/)
    - [Patterns](https://wordpress.org/patterns/)
    - [Photos](https://wordpress.org/photos/)
    - [Openverse ︎](https://openverse.org/)
    - [WordPress.tv ︎](https://wordpress.tv/)
 *  - [About WordPress](https://wordpress.org/about/)
    - [Make WordPress](https://make.wordpress.org/)
    - [Events](https://events.wordpress.org/)
    - [Five for the Future](https://wordpress.org/five-for-the-future/)
    - [Enterprise](https://wordpress.org/enterprise/)
    - [Gutenberg ︎](https://wordpress.org/gutenberg/)
    - [Job Board ︎](https://jobs.wordpress.net/)
 * [Swag ︎](https://mercantile.wordpress.org/)
 * [Get WordPress](https://wordpress.org/download/)

Search in WordPress.org

 [ Get WordPress ](https://wordpress.org/download/)

[WordPress Developer Blog](https://developer.wordpress.org/news)

 * [Snippets](https://developer.wordpress.org/news/snippets/)
    - [Blocks](https://developer.wordpress.org/news/category/blocks/)
    - [Concepts](https://developer.wordpress.org/news/category/concepts/)
    - [Common APIs](https://developer.wordpress.org/news/category/common-apis/)
    - [Design](https://developer.wordpress.org/news/category/design/)
    - [Playground](https://developer.wordpress.org/news/category/playground/)
    - [Plugins](https://developer.wordpress.org/news/category/plugins/)
    - [Themes](https://developer.wordpress.org/news/category/themes/)
 *  - [About this site](https://developer.wordpress.org/news/about/)
    - [Updates](https://developer.wordpress.org/news/category/updates/)
    - [How to contribute](https://developer.wordpress.org/news/how-to-contribute/)
    - [Tips and guidelines](https://developer.wordpress.org/news/tips-and-guidelines-for-writers/)
 * [Subscribe](https://developer.wordpress.org/news/subscribe/)
 * [Developer Resources](https://developer.wordpress.org/)

 * [Snippets](https://developer.wordpress.org/news/snippets/)
    - [Blocks](https://developer.wordpress.org/news/category/blocks/)
    - [Concepts](https://developer.wordpress.org/news/category/concepts/)
    - [Common APIs](https://developer.wordpress.org/news/category/common-apis/)
    - [Design](https://developer.wordpress.org/news/category/design/)
    - [Playground](https://developer.wordpress.org/news/category/playground/)
    - [Plugins](https://developer.wordpress.org/news/category/plugins/)
    - [Themes](https://developer.wordpress.org/news/category/themes/)
 *  - [About this site](https://developer.wordpress.org/news/about/)
    - [Updates](https://developer.wordpress.org/news/category/updates/)
    - [How to contribute](https://developer.wordpress.org/news/how-to-contribute/)
    - [Tips and guidelines](https://developer.wordpress.org/news/tips-and-guidelines-for-writers/)
 * [Subscribe](https://developer.wordpress.org/news/subscribe/)
 * [Developer Resources](https://developer.wordpress.org/)

# 󠀁[Application state managed withDispatch, withSelect and compose 101](https://developer.wordpress.org/news/2022/12/application-state-managed-withdispatch-withselect-and-compose-101/)󠁿

By [Dean Sas](https://profiles.wordpress.org/dsas/)

December 13, 2022

[Advanced](https://developer.wordpress.org/news/category/advanced/)

Editor related Javascript code is full of references to withDispatch, withSelect
and compose. This article briefly explains why these methods are important, what
they’re used for and how to use them.

withDispatch, withSelect and compose are a triad of Gutenberg methods that are often
used together to manage application state. They are how Gutenberg component and 
blocks read and modify editor data from the outside of the component.

You should know that **useSelect and useDispatch hooks are a more modern way of 
managing application state** but the with* methods are widespread across Gutenberg
and custom editor code, and likely will be for sometime, so being able to read them
is worthwhile. The main advantage of the use* hooks is that they make for more readable
code. It’s relatively straight-forward to switch to the modern use* hooks rather
than the with* methods.

## **withDispatch**

You pass [withDispatch](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-data/#withdispatch)
a function which receives a dispatch method. The purpose of the function is to return
a list of dispatch (data modification) methods for the components use.

    ```javascript
    withDispatch( dispatch => {    
    	return { replaceBlock, selectBlock } = dispatch( 'core/block-editor' );
    } )
    ```

In the above example it’s returning the `replaceBlock` and `selectBlock` methods
from the core/block-editor data store. They will be passed on as a prop to your 
component, so you can use them when you need to change state.

The methods returned are just methods, they don’t need to be existing ones but could
be wrappers around them, like in this example below from the [Jetpack recurring-payments block](https://github.com/Automattic/jetpack/blob/196315a4c7c8889c6a0e0ed8273cf228cb98eb36/projects/plugins/jetpack/extensions/blocks/premium-content/buttons/edit.js#L109-L120).

    ```javascript
    withDispatch( ( dispatch, props ) => ( {    
    	/**     
    	 * Updates the plan on the Recurring Payments block acting as a subscribe button.        
    	 * @param {number} planId - Plan ID.     
    	 */    
    	setSubscribeButtonPlan( planId ) {        
    		dispatch( 'core/block-editor' ).updateBlockAttributes(
    			props.subscribeButton.clientId,
    			{ planId }
    		);      
    	},
    } ) ),
    ```

In the browser dev console it is possible to use `wp.data.dispatch( store_name )`
directly for debugging purposes.

## **withSelect**

You pass [withSelect](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-data/#withselect)
a function which receives a select method. The purpose of the function is to return
a list of select (data query) values that the component needs.

    ```javascript
    withSelect( ( select ) => ( {
    	postId: select( 'core/editor' ).getCurrentPostId(),
    } ) );
    ```

In the above example it’s returning the postId of the post currently being edited,
it gets this from the core/editor store.

The postId will be passed on to the component as a prop. The value of the prop will
then automatically be changed inside the component as the value returned by the 
selector changes.

In the browser dev console it is possible to use `wp.data.select( store_name )` 
directly for debugging purposes.

## **compose**

[compose](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-compose/#compose)
is passed an array of methods, it runs each of these methods in turn starting from
the right. The result of each method is passed into the next method. The below two
JavaScript snippets are identical.

    ```javascript
    compose( [ methodOne, methodTwo, methodThree ] ) ()
    ```

    ```javascript
    methodOne( methodTwo( methodThree() ) )
    ```

withSelect and withDispatch are usually called within the compose method like this:

    ```javascript
    // Where your_component_class_or_function is a standard class/function receiving props
    export default compose ( [
    	withSelect( the_with_select_method ),   
    	withDispatch( the_withDispatch_method )
    ] ) ( your_component_class_or_fn )
    ```

The component you pass then will have the values returned from the methods passed
to withSelect and withDispatch available as props.

## **Comparing use and with methods**

use* hooks are a more modern way of achieving the same thing, they are usually preferred
to with* methods because they are more succinct and it easier to read. It’s easier
to read because use* places the definition of the values next to where the values
are used, whereas with* places the use of the values inside the component and the
definition of the values at the bottom of the file, outside of the component.

In addition, the compose method is no longer needed at all. Rather than passing 
the values and functions as props via compose, the values and methods are available
within the use of the hook.

Below is a brief made-up example to illustrate the differences between the two approaches.

### **with\***

    ```javascript
    function MyAwesomeComponent( {
    	// The values from the with* methods are passed as props
    	clientId,
    	defaultVariation,
    	hasInnerBlocks,
    	replaceBlock,
    	selectBlock
    } ) {
    	// A made-up example using the props.
    	const replaceEmptyBlock = () => {
    		if ( ! hasInnerBlocks && ‘flibble/baz’ === defaultVariation ) {
    			replaceBlock( clientId, ‘flibble/block’ );
    			selectBlock( clientId );
    		}
    	}
    }
    export default compose ( [
    	withSelect( (select, props ) => {
    		const { getDefaultBlockVariation } = select( blocksStore );
    		const { getBlocks } = select( blockEditorStore );
    		return {
    			defaultVariation: getDefaultBlockVariation( props.name );
    			hasInnerBlocks: getBlocks( props.clientId )?.length > 0,
    		};
    	} ),
    	withDispatch( ( select ) => {
    		const { replaceBlock, selectBlock } = dispatch( blockEditorStore );
    		return { replaceBlock, selectBlock }
    	} )
    ] )( MyAwesomeComponent );
    ```

### **use\***

    ```javascript
    export function MyAwesomeComponent( { name, clientId } ) {
    	// The values from useSelect are fetched and are immediately available rather than being passed as props
    	const { defaultVariation, hasInnerBlocks } = useSelect( select => {
    		const { getDefaultBlockVariation } = select( blocksStore );
    		const { getBlocks } = select( blockEditorStore );
    		return {
    			defaultVariation: getDefaultBlockVariation( name ),
    			hasInnerBlocks: getBlocks( clientId )?.length > 0,
    		};
    	}, [ name, clientId ] );

    	const { replaceBlock, selectBlock } = useDispatch( blockEditorStore );

    	// A made-up example using the props.
    	const replaceEmptyBlock = () => {
    		if ( ! hasInnerBlocks && ‘flibble/baz’ === defaultVariation ) {
    			replaceBlock( clientId, ‘flibble/quux’ );
    			selectBlock( clientId );
    		}
    	}
    }
    ```

One other difference you might have noticed is that `[ name, clientId ]` is passed
to `useSelect` as the second parameter. These are variables come from outside of
useSelect but are used inside the first parameter, passing them as the second parameter
means that useSelect will use them to memoize (cache) the first parameter.

## **Further reading**

 * [List of core data stores](https://developer.wordpress.org/block-editor/reference-guides/data/)
 * [Example of real world code using withSelect methods](https://github.com/Automattic/jetpack/blob/196315a4c7c8889c6a0e0ed8273cf228cb98eb36/projects/plugins/jetpack/extensions/blocks/premium-content/buttons/edit.js)
 * [useDispatch](https://github.com/WordPress/gutenberg/blob/a55f8809c0aecb7ce781ea51f19f8e388b929b00/packages/data/src/components/use-dispatch/use-dispatch.js)
   source with examples
 * [useSelect](https://github.com/WordPress/gutenberg/blob/9b136bbc95815ff2bfa2a96a4af258b402d715d8/packages/data/src/components/use-select/index.js)
   source with examples
 * [Example of real world code using the use* methods](https://github.com/WordPress/gutenberg/blob/8bbbba37a7a181afeb724bc2d3421b1e77d52c99/packages/block-library/src/query/edit/query-placeholder.js)

Don’t miss new posts on this site. Subscribe.

_With thanks to _[@bph](https://profiles.wordpress.org/bph/)_, [Milana Cap](https://profiles.wordpress.org/milana_cap/)
and [Marco Ciampini](https://profiles.wordpress.org/mciampini/) for reviewing earlier
drafts of this article. All errors are mine._

**Categories:** [Advanced](https://developer.wordpress.org/news/category/advanced/)

## One response to “Application state managed withDispatch, withSelect and compose 101”

 1. [Master Styles and Block variations in Gutenberg](https://kinsta.com/blog/style-block-variations-gutenberg/)
 2. [August 7, 2025](https://developer.wordpress.org/news/2022/12/application-state-managed-withdispatch-withselect-and-compose-101/#comment-19152)
 3. […] useDispatch from @wordpress/data. (See WordPress Developer Blog) […]
 4. [Reply](https://developer.wordpress.org/news/2022/12/application-state-managed-withdispatch-withselect-and-compose-101/?replytocom=19152#respond)

### Leave a Reply 󠀁[Cancel reply](https://developer.wordpress.org/news/2022/12/application-state-managed-withdispatch-withselect-and-compose-101/?output_format=md#respond)󠁿

Your email address will not be published. Required fields are marked *

Comment *

Name *

Email *

Website

 Save my name, email, and website in this browser for the next time I comment.

 Notify me of follow-up comments by email.

 Notify me of new posts by email.

Δ

## Have an idea for a new post?

### 󠀁[Learn how to contribute](https://developer.wordpress.org/news/how-to-contribute/)󠁿

Share your knowledge with fellow WordPress developers.

Search

### 󠀁[Review tips and guidelines](https://developer.wordpress.org/news/tips-and-guidelines-for-writers/)󠁿

Everything you need to know about writing for the Blog.

## Subscribe to the Blog

  Email Address

 Join 1,946 other subscribers

 * [About](https://wordpress.org/about/)
 * [News](https://wordpress.org/news/)
 * [Hosting](https://wordpress.org/hosting/)
 * [Privacy](https://wordpress.org/about/privacy/)

 * [Showcase](https://wordpress.org/showcase/)
 * [Themes](https://wordpress.org/themes/)
 * [Plugins](https://wordpress.org/plugins/)
 * [Patterns](https://wordpress.org/patterns/)

 * [Learn](https://learn.wordpress.org/)
 * [Documentation](https://wordpress.org/documentation/)
 * [Developers](https://developer.wordpress.org/)
 * [WordPress.tv ](https://wordpress.tv/)

 * [Get Involved](https://make.wordpress.org/)
 * [Events](https://events.wordpress.org/)
 * [Donate ](https://wordpressfoundation.org/donate/)
 * [Swag ](https://mercantile.wordpress.org/)

 * [WordPress.com ](https://wordpress.com/?ref=wporg-footer)
 * [Matt ](https://ma.tt/)
 * [bbPress ](https://bbpress.org/)
 * [BuddyPress ](https://buddypress.org/)

 The WordPress® trademark is the intellectual property of the WordPress Foundation.

 * [Visit our X (formerly Twitter) account](https://www.x.com/WordPress)
 * [Visit our Bluesky account](https://bsky.app/profile/wordpress.org)
 * [Visit our Mastodon account](https://mastodon.world/@WordPress)
 * [Visit our Threads account](https://www.threads.net/@wordpress)
 * [Visit our Facebook page](https://www.facebook.com/WordPress/)
 * [Visit our Instagram account](https://www.instagram.com/wordpress/)
 * [Visit our LinkedIn account](https://www.linkedin.com/company/wordpress)
 * [Visit our TikTok account](https://www.tiktok.com/@wordpress)
 * [Visit our YouTube channel](https://www.youtube.com/wordpress)
 * [Visit our Tumblr account](https://wordpress.tumblr.com/)