@automattic/data-stores
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

Calypso Data Stores

This package contains a collection of @wordpress/data-based stores that can be used to fetch data from various WordPress.com REST API endpoints.

It is meant to be helpful for projects developed inside the Calypso monorepo that don't want to use Calypso's (monolithic) Redux state tree.

Usage

To use stores from the package, import and register the relevant store to obtain its key:

import { Verticals } from '@automattic/data-stores';
import { useSelect } from '@wordpress/data';

const VERTICALS_STORE = Verticals.register();

export const VerticalSelect = (): React.FunctionComponent<  > => {
	const verticals = useSelect( ( select ) => select( VERTICALS_STORE ).getVerticals() );

	return (
		<ul>
			{ verticals.map( ( vertical ) => (
				<li key={ vertical.vertical_id }>{ vertical.vertical_name }</li>
			) ) }
		</ul>
	);
};

A note about store register functions

Stores are not registered in their module code. Instead, they expose register functions. This is an important technical consideration to avoid side effects and allow tree-shaking.

When implementing a store, registration on evaluation should be avoided. Please, follow the pattern of exporting a register function.

When an application needs to use a store that should be configured, it may be helpful to expose that store from a wrapper module:

// vertical-store.ts
import { DomainSuggestions } from '@automattic/data-stores';
export const DOMAIN_SUGGESTIONS_STORE = DomainSuggestions.register( {
	/* …my application configuration… */
} );
// elsewhere…
import { DOMAIN_SUGGESTIONS_STORE } from './vertical-store';
select( DOMAIN_SUGGESTIONS_STORE ).getCategories();

Types

The stores in this package are written in TypeScript, and type definitions are generated as part of the build process. Furthermore, we're injecting type information for available selectors and actions into the @wordpress/data module, which means that you'll get handy autocomplete suggestions, if you're writing your project in TypeScript and you've enabled your editor's TypeScript feature or plugin.

autocomplete

Readme

Keywords

none

Package Sidebar

Install

npm i @automattic/data-stores

Weekly Downloads

12,911

Version

3.0.1

License

GPL-2.0-or-later

Unpacked Size

1.81 MB

Total Files

943

Last publish

Collaborators

  • porada
  • imranh920
  • gmjuhasz
  • rcarvalho
  • briowill
  • bgrgicak
  • dhenridev
  • daledupreez-a8c
  • jeherve
  • yuliyan
  • micbosia8c
  • jeremy.yip
  • wpvip-bot
  • etobiesen
  • kzoschke
  • brunobasto
  • kat3samsin
  • fmfernandes
  • newspack
  • robertsreberski_a8c
  • msurdi-a8c
  • chihsuan
  • manzoorwanijk
  • rjchow
  • andrea-sdl
  • scjr
  • spsiddarthan
  • t2dw4t
  • ehg_
  • wwa
  • sirreal
  • elazzabi
  • royho
  • luismulinari
  • macbre
  • mjangda
  • matticbot
  • a8c
  • blowery
  • noahtallen
  • hanifn
  • sgomes
  • tyxla
  • saroshaga
  • parkcityj
  • nejclovrencic
  • sirbrillig
  • chriszarate
  • robersongomes
  • johngodley