The useStoreNotices()
hook allows reading and manipulating notices in the frontend.
Please refer to the useStoreSnackbarNotices()
section for information on handling snackbar notices.
Note: if the context is not specified in
noticeProps
orctxt
params (depending on the method), the current context is used.
Create a new notice with default
status.
Argument | Type | Description |
---|---|---|
text |
string | Text to be displayed in the notice. |
noticeProps |
Object | Object with the notice options. |
Create a new error notice.
Argument | Type | Description |
---|---|---|
text |
string | Text to be displayed in the notice. |
noticeProps |
Object | Object with the notice options. |
Create a new warning notice.
Argument | Type | Description |
---|---|---|
text |
string | Text to be displayed in the notice. |
noticeProps |
Object | Object with the notice options. |
Create a new info notice.
Argument | Type | Description |
---|---|---|
text |
string | Text to be displayed in the notice. |
noticeProps |
Object | Object with the notice options. |
Create a new success notice.
Argument | Type | Description |
---|---|---|
text |
string | Text to be displayed in the notice. |
noticeProps |
Object | Object with the notice options. |
Check whether there are notices of the provided type in the current context.
Argument | Type | Description |
---|---|---|
type |
string | Type of the notices to check. |
An array of the notices in the current context.
Remove an existing notice.
Argument | Type | Description |
---|---|---|
id |
string | Id of the notice to remove. |
ctx |
string | Context where the notice to remove is stored. |
Remove all notices from the current context. If a status
is provided, only the notices with that status are removed.
Argument | Type | Description |
---|---|---|
status |
string | Status that notices must match to be removed. If not provided, all notices of any status are removed. |
Whether notices are suppressed. If true, it will hide the notices from the frontend.
Argument | Type | Description |
---|---|---|
val |
boolean | Id of the notice to remove. |
The StoreNoticesProvider
allows managing notices in the frontend. Notices are rendered before React Children.
Internally, it uses the StoreNoticesContext
which relies on the notices
package from Gutenberg.
This action creates a new notice. If the context is not specified in the options
object, the current context is used.
Argument | Type | Description |
---|---|---|
status |
string | One of the statuses listed below. |
content |
string | Text to be displayed in the notice. |
options |
Object | Object with the notice options. |
This action creates a new snackbar notice. If the context is not specified in the options
object, the current context is used.
Argument | Type | Description |
---|---|---|
content |
string | Text to be displayed in the notice. |
options |
Object | Object with the notice options. |
This action removes an existing notice. If the context is not specified, the current context is used.
Argument | Type | Description |
---|---|---|
id |
string | Id of the notice to remove. |
ctx |
string | Context where the notice to remove is stored. If the context is not specified, the current context is used. |
Whether notices are suppressed. If true, it will hide the notices from the frontend.
Argument | Type | Description |
---|---|---|
val |
boolean | Id of the notice to remove. |
All notices must have one of the following statuses: default
, error
, success
, info
, warning
.
Object of the form:
{
id: 'checkout',
type: string,
isDismissible: false,
}
Refer to the Gutenberg docs to know the available options.
The useStoreNotices()
hook allows reading and manipulating snackbar notices in the frontend.
The snackbar is a small toast-like notification that appears at the bottom of a user's screen.
Create a new snackbar notice.
Argument | Type | Description |
---|---|---|
text |
string | Text to be displayed in the notice. |
noticeProps |
Object | Object with the notice options. |
An array of the notices in the current context.
Remove all notices from the current context. If a status
is provided, only the notices with that status are removed.
Argument | Type | Description |
---|---|---|
status |
string | Status that notices must match to be removed. If not provided, all notices of any status are removed. |
The StoreSnackbarNoticesProvider
allows managing snackbar notices in the frontend. Snackbar notices are displayed in the bottom left corner and disappear after a certain time.
Internally, it uses the StoreNoticesContext
which relies on the notices
package from Gutenberg.
This action creates a new snackbar notice. If the context is not specified in the options
object, the current context is used.
Argument | Type | Description |
---|---|---|
content |
string | Text to be displayed in the notice. |
options |
Object | Object with the notice options. |
This action removes an existing notice. If the context is not specified, the current context is used.
Argument | Type | Description |
---|---|---|
id |
string | Id of the notice to remove. |
ctx |
string | Context where the notice to remove is stored. If the context is not specified, the current context is used. |
Whether notices are suppressed. If true, it will hide the notices from the frontend.
Argument | Type | Description |
---|---|---|
val |
boolean | Id of the notice to remove. |
The following example shows a CheckoutProcessor
component that displays an error notice when the payment process fails and it removes it every time the payment is started. When the payment is completed correctly, it shows a snackbar notice.
const CheckoutProcessor = () => {
const { addErrorNotice, removeNotice } = useStoreNotices();
const { addSnackbarNotice } = useStoreSnackbarNotices();
// ...
const paymentFail = () => {
addErrorNotice( 'Something went wrong.', { id: 'checkout' } );
};
const paymentStart = () => {
removeNotice( 'checkout' );
};
const paymentSuccess = () => {
addSnackbarNotice( 'Payment successfully completed.' );
};
// ...
};
<StoreNoticesSnackbarProvider context="wc/checkout">
<StoreNoticesProvider context="wc/checkout">
// ...
<CheckoutProcessor />
</StoreNoticesProvider>
</StoreSnackbarNoticesProvider>