Gutenberg 区块编辑器文档

title: "通知数据" post_status: publish comment_status: open taxonomy: category: - gutenberg-docs post_tag: - Data - Reference Guides - Repos


通知数据

命名空间:core/notices

选择器

getNotices

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

用法

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>
    );
};

参数

返回值

操作

createErrorNotice

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

相关

用法

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>
    );
};

参数

返回值

createInfoNotice

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

相关

用法

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>
    );
};

参数

返回值

createNotice

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

用法

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>
    );
};

参数

返回值

createSuccessNotice

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

相关

用法

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>
    );
};

参数

返回值

createWarningNotice

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

相关

用法

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>
    );
};

参数

返回值

removeAllNotices

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

用法

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>
        </>
    );
};

参数

返回值

removeNotice

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

用法

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>
            ) }
        </>
    );
};

参数

返回值

removeNotices

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

用法

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>
        </>
    );
};

参数

返回值