# 通知数据

命名空间：`core/notices`。

## 选择器

<!-- START TOKEN(Autogenerated selectors|../../../packages/notices/src/store/selectors.ts) -->

### getNotices

返回所有通知的数组，可选择针对给定的上下文。默认为全局上下文。

_用法_

```js
import { useSelect } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';

const ExampleComponent = () => {
	const notices = useSelect( ( select ) =>
		select( noticesStore ).getNotices()
	);
	return (
		<ul>
			{ notices.map( ( notice ) => (
				<li key={ notice.ID }>{ notice.content }</li>
			) ) }
		</ul>
	);
};
```

_参数_

-   _state_ `Record< string, Array< Notice > >`: 通知状态。
-   _context_ `string`: 可选的分组上下文。

_返回值_

-   通知数组。

<!-- END TOKEN(Autogenerated selectors|../../../packages/notices/src/store/selectors.ts) -->

## 操作

<!-- START TOKEN(Autogenerated actions|../../../packages/notices/src/store/actions.ts) -->

### createErrorNotice

返回一个用于发出信号以创建错误通知的操作对象。有关选项的文档，请参阅 `createNotice`。

_相关_

-   createNotice

_用法_

```js
import { __ } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { Button } from '@wordpress/components';

const ExampleComponent = () => {
	const { createErrorNotice } = useDispatch( noticesStore );
	return (
		<Button
			onClick={ () =>
				createErrorNotice( __( 'An error occurred!' ), {
					type: 'snackbar',
					explicitDismiss: true,
				} )
			}
		>
			{ __(
				'Generate a snackbar error notice with explicit dismiss button.'
			) }
		</Button>
	);
};
```

_参数_

-   _content_ `string`: 通知消息。
-   _options_ `NoticeOptions`: 可选的通知选项。

_返回值_

-   操作对象。

### createInfoNotice

返回一个用于指示创建信息通知的操作对象。选项文档请参阅 `createNotice`。

_相关_

-   createNotice

_用法_

```js
import { __ } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { Button } from '@wordpress/components';

const ExampleComponent = () => {
	const { createInfoNotice } = useDispatch( noticesStore );
	return (
		<Button
			onClick={ () =>
				createInfoNotice( __( 'Something happened!' ), {
					isDismissible: false,
				} )
			}
		>
			{ __( 'Generate a notice that cannot be dismissed.' ) }
		</Button>
	);
};
```

_参数_

-   _content_ `string`: 通知消息。
-   _options_ `NoticeOptions`: 可选的通知选项。

_返回值_

-   操作对象。

### createNotice

返回一个用于发出创建通知信号的动作对象。

_用法_

```js
import { __ } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { Button } from '@wordpress/components';

const ExampleComponent = () => {
	const { createNotice } = useDispatch( noticesStore );
	return (
		<Button
			onClick={ () => createNotice( 'success', __( 'Notice message' ) ) }
		>
			{ __( 'Generate a success notice!' ) }
		</Button>
	);
};
```

_参数_

-   _status_ 通知状态（如果传入 `undefined`，则为 "info"）。
-   _content_ `string`: 通知消息。
-   _options_ `NoticeOptions`: 可选的通知选项。

_返回值_

-   `Extract< ReducerAction, { type: 'CREATE_NOTICE'; } >`: 动作对象。

### createSuccessNotice

返回一个用于指示需要创建成功通知的操作对象。有关选项文档，请参阅 `createNotice`。

_相关_

-   createNotice

_用法_

```js
import { __ } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { Button } from '@wordpress/components';

const ExampleComponent = () => {
	const { createSuccessNotice } = useDispatch( noticesStore );
	return (
		<Button
			onClick={ () =>
				createSuccessNotice( __( 'Success!' ), {
					type: 'snackbar',
					icon: '🔥',
				} )
			}
		>
			{ __( 'Generate a snackbar success notice!' ) }
		</Button>
	);
};
```

_参数_

-   _content_ `string`: 通知消息。
-   _options_ `NoticeOptions`: 可选的通知选项。

_返回值_

-   操作对象。

### createWarningNotice

返回一个用于发出创建警告通知信号的动作对象。有关选项文档，请参阅 `createNotice`。

_相关_

-   createNotice

_用法_

```js
import { __ } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { Button } from '@wordpress/components';

const ExampleComponent = () => {
	const { createWarningNotice, createInfoNotice } =
		useDispatch( noticesStore );
	return (
		<Button
			onClick={ () =>
				createWarningNotice( __( 'Warning!' ), {
					onDismiss: () => {
						createInfoNotice(
							__( 'The warning has been dismissed!' )
						);
					},
				} )
			}
		>
			{ __( 'Generates a warning notice with onDismiss callback' ) }
		</Button>
	);
};
```

_参数_

-   _content_ `string`: 通知消息。
-   _options_ `NoticeOptions`: 可选的通知选项。

_返回值_

-   动作对象。

### removeAllNotices

移除指定上下文中的所有通知。默认为默认上下文。

_用法_

```js
import { __ } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { Button } from '@wordpress/components';

export const ExampleComponent = () => {
	const notices = useSelect( ( select ) =>
		select( noticesStore ).getNotices()
	);
	const { removeAllNotices } = useDispatch( noticesStore );
	return (
		<>
			<ul>
				{ notices.map( ( notice ) => (
					<li key={ notice.id }>{ notice.content }</li>
				) ) }
			</ul>
			<Button onClick={ () => removeAllNotices() }>
				{ __( 'Clear all notices', 'woo-gutenberg-products-block' ) }
			</Button>
			<Button onClick={ () => removeAllNotices( 'snackbar' ) }>
				{ __(
					'Clear all snackbar notices',
					'woo-gutenberg-products-block'
				) }
			</Button>
		</>
	);
};
```

_参数_

-   _noticeType_ 要移除所有通知的上下文。
-   _context_ `string`: 可选的上下文，用于移除所有通知。

_返回值_

-   `Extract< ReducerAction, { type: 'REMOVE_ALL_NOTICES'; } >`: 操作对象。

### removeNotice

返回一个用于发出移除通知信号的动作对象。

_用法_

```js
import { __ } from '@wordpress/i18n';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { Button } from '@wordpress/components';

const ExampleComponent = () => {
	const notices = useSelect( ( select ) =>
		select( noticesStore ).getNotices()
	);
	const { createWarningNotice, removeNotice } = useDispatch( noticesStore );

	return (
		<>
			<Button
				onClick={ () =>
					createWarningNotice( __( 'Warning!' ), {
						isDismissible: false,
					} )
				}
			>
				{ __( 'Generate a notice' ) }
			</Button>
			{ notices.length > 0 && (
				<Button onClick={ () => removeNotice( notices[ 0 ].id ) }>
					{ __( 'Remove the notice' ) }
				</Button>
			) }
		</>
	);
};
```

_参数_

-   _id_ `string`: 通知的唯一标识符。
-   _context_ `string`: 可选参数，通知预期出现的上下文（分组）。默认为 'default' 上下文。

_返回值_

-   `Extract< ReducerAction, { type: 'REMOVE_NOTICE'; } >`: 动作对象。

### removeNotices

返回一个用于指示需要移除多个通知的动作对象。

_用法_

```js
import { __ } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
import { Button } from '@wordpress/components';

const ExampleComponent = () => {
	const notices = useSelect( ( select ) =>
		select( noticesStore ).getNotices()
	);
	const { removeNotices } = useDispatch( noticesStore );
	return (
		<>
			<ul>
				{ notices.map( ( notice ) => (
					<li key={ notice.id }>{ notice.content }</li>
				) ) }
			</ul>
			<Button
				onClick={ () =>
					removeNotices( notices.map( ( { id } ) => id ) )
				}
			>
				{ __( 'Clear all notices' ) }
			</Button>
		</>
	);
};
```

_参数_

-   _ids_ `Array< string >`: 唯一通知标识符的列表。
-   _context_ `string`: 可选参数，表示通知预期出现的上下文（分组）。默认为 'default' 上下文。

_返回值_

-   `Extract< ReducerAction, { type: 'REMOVE_NOTICES'; } >`: 动作对象。

<!-- END TOKEN(Autogenerated actions|../../../packages/notices/src/store/actions.ts) -->