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

Adds documentation about selectors #52941

Merged
merged 11 commits into from Jul 28, 2023
99 changes: 99 additions & 0 deletions packages/data/README.md
Expand Up @@ -1028,6 +1028,105 @@ function Component() {
}
```

## Selectors

The following selectors are available on the object returned by `wp.data.select( 'core' )`.

_Example_

```js
import { store as coreDataStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';

function Component() {
const result = useSelect( ( select ) => {
const query = { per_page: 20 };
const selectorArgs = [ 'postType', 'page', query ];

return {
pages: select( coreDataStore ).getEntityRecords( ...selectorArgs ),
hasStartedResolution: select( coreDataStore ).hasStartedResolution(
'getEntityRecords', // _selectorName_
selectorArgs
),
hasFinishedResolution: select(
coreDataStore
).hasFinishedResolution( 'getEntityRecords', selectorArgs ),
isResolving: select( coreDataStore ).isResolving(
'getEntityRecords',
selectorArgs
),
};
} );

if ( result.hasStartedResolution ) {
return <>Fetching data...</>;
}

if ( result.isResolving ) {
return (
<>
{
// show a spinner
}
</>
);
}

if ( result.hasFinishedResolution ) {
return (
<>
{
// data is ready
}
</>
);
}
}
```

### hasFinishedResolution

Returns true if resolution has completed for a given selector name, and arguments set.

_Parameters_

- _state_ `State`: Data state.
- _selectorName_ `string`: Selector name.
- _args_ `unknown[]?`: Arguments passed to selector.

_Returns_

- `boolean`: Whether resolution has completed.

### hasStartedResolution

Returns true if resolution has already been triggered for a given selector name, and arguments set.

_Parameters_

- _state_ `State`: Data state.
- _selectorName_ `string`: Selector name.
- _args_ `unknown[]?`: Arguments passed to selector.

_Returns_

- `boolean`: Whether resolution has been triggered.

### isResolving

Returns true if resolution has been triggered but has not yet completed for a given selector name, and arguments set.

_Parameters_

- _state_ `State`: Data state.
- _selectorName_ `string`: Selector name.
- _args_ `unknown[]?`: Arguments passed to selector.

_Returns_

- `boolean`: Whether resolution is in progress.

## Going further

- [What is WordPress Data?](https://unfoldingneurons.com/2020/what-is-wordpress-data/)
Expand Down