Skip to content

Commit

Permalink
update to README
Browse files Browse the repository at this point in the history
  • Loading branch information
nerrad committed May 31, 2019
1 parent 0173348 commit 70f2bea
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions packages/data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,52 @@ _Parameters_

Undocumented declaration.

<a name="useDispatch" href="#useDispatch">#</a> **useDispatch**

A custom react hook returning the current registry dispatch actions creators.

Note: The component using this hook must be within the context of a
RegistryProvider.

_Usage_

This illustrates a pattern where you may need to retrieve dynamic data from
the server via the `useSelect` hook to use in combination with the dispatch
action.

```jsx
const { useDispatch, useSelect } = wp.data;
const { useCallback } = wp.element;

function Button( { onClick, children } ) {
return <button type="button" onClick={ onClick }>{ children }</button>
}

const SaleButton = ( { children } ) => {
const { stockNumber } = useSelect(
( select ) => select( 'my-shop' ).getStockNumber()
);
const { startSale } = useDispatch( 'my-shop' );
const onClick = useCallback( () => {
const discountPercent = stockNumber > 50 ? 10: 20;
startSale( discountPercent );
}, [ stockNumber ] );
return <Button onClick={ onClick }>{ children }</Button>
}

// Rendered somewhere in the application:
//
// <SaleButton>Start Sale!</SaleButton>
```
_Parameters_
- _storeName_ `?string`: Optionally provide the name of the store from which to retrieve action creators. If not provided, the registry.dispatch function is returned instead.
_Returns_
- `Function`: A custom react hook.
<a name="useRegistry" href="#useRegistry">#</a> **useRegistry**
A custom react hook exposing the registry context for use.
Expand Down

0 comments on commit 70f2bea

Please sign in to comment.