withNotices 是一个 React 高阶组件,用于为原始组件添加发布通知消息的功能。通过包装原始组件,它提供了 noticeOperations、noticeUI 和 noticeList 等额外属性。
import { withNotices, Button } from '@wordpress/components';
const MyComponentWithNotices = withNotices(
( { noticeOperations, noticeUI } ) => {
const addError = () =>
noticeOperations.createErrorNotice( 'Error message' );
return (
<div>
{ noticeUI }
<Button variant="secondary" onClick={ addError }>
Add error
</Button>
</div>
);
}
); withNotices is a React higher-order component used typically in adding the ability to post notice messages within the original component.
Wrapping the original component with withNotices encapsulates the component with the additional props noticeOperations, noticeUI, and noticeList.
noticeOperations
Contains a number of useful functions to add notices to your site.
# createNotice
Function passed down as a prop that adds a new notice.
Parameters
object: Notice to add.# createErrorNotice
Function passed as a prop that adds a new error notice.
Parameters
string: Error message of the notice.# removeAllNotices
Function that removes all notices.
# removeNotice
Function that removes notice by ID.
Parameters
string: ID of notice to remove.#noticeUi
The rendered NoticeList.
#noticeList
The array of notice objects to be displayed.
import { withNotices, Button } from '@wordpress/components';
const MyComponentWithNotices = withNotices(
( { noticeOperations, noticeUI } ) => {
const addError = () =>
noticeOperations.createErrorNotice( 'Error message' );
return (
<div>
{ noticeUI }
<Button variant="secondary" onClick={ addError }>
Add error
</Button>
</div>
);
}
);