store-notices
ストア通知ストア (wc/store/store-notices)
概要
お知らせストアでは、お知らせ用のコンテナを登録したり解除したりできます。これはカスタムブロックのような特定の場所にお知らせを表示するのに便利です。
このストアを利用するには、storeNoticesStore StoreDescriptor を参照するモジュールでインポートします。@woocommerce/block-data が wc.wcBlocksData を指す外部として登録されていると仮定すると、StoreDescriptor をインポートすることができます:
import { storeNoticesStore } from '@woocommerce/block-data';
そうでない場合は、このようにウィンドウからアクセスする:
const { storeNoticesStore } = window.wc.wcBlocksData;
以下のコード・スニペットは、通知用コンテナの登録方法を示している。
import { store as noticesStore } from '@wordpress/notices';
export default function Block( attributes ) {
const context = 'your-namespace/custom-form-step';
dispatch( noticesStore ).createNotice(
'error',
'This is an example of an error notice.',
{ context }
);
return (
<>
<StoreNoticesContainer context={ context } />
{ /* Your custom block code here */ }
</>
);
}
内部的には、
StoreNoticesContainerコンポーネントがregisterContainerアクションをディスパッチします。
これは単純な例であることに注意してください。実際には、フォームを送信するなどのユーザーアクションに応じてcreateNoticeアクションをトリガーしたいでしょう。
登録コンテナ( containerContext )
このアクションは新しいコンテナを登録する。
Parameters
- containerContext
string: 登録するコンテナのコンテキストまたは識別子。
Returns
object: 以下のプロパティを持つアクションオブジェクト- type
string: アクションのタイプ。 - containerContext
string: 渡された_containerContext_。
- type
例
import { storeNoticesStore } from '@woocommerce/block-data';
dispatch( storeNoticesStore ).registerContainer( 'someContainerContext' );
unregisterContainer( containerContext )
このアクションは、既存のコンテナの登録を解除する。
Parameters
- containerContext
string: 登録解除するコンテナのコンテキストまたは識別子。
Returns
object: 以下のプロパティを持つアクションオブジェクト:- type
string: アクションのタイプ。 - containerContext
string: 渡された containerContext。
- type
例
import { storeNoticesStore } from '@woocommerce/block-data';
dispatch( storeNoticesStore ).unregisterContainer( 'someContainerContext' );
セレクタ
getRegisteredContainers
州から現在登録されているコンテナのリストを返す。
Returns
string[]: 登録されているコンテナコンテキストを表す文字列の配列。
例
import { storeNoticesStore } from '@woocommerce/block-data';
const store = select( storeNoticesStore );
const registeredContainers = store.getRegisteredContainers();
