I’m making an attempt to make use of withSelect and withDispatch to show an admin discover when "save" button is clicked. I’m utilizing the next code from this repo but it surely throws an error: "notices is undefined". Right here is the code I’m utilizing:
import { Icon, Button, SnackbarList } from '@wordpress/parts';
import { dispatch, withSelect, withDispatch } from '@wordpress/knowledge';
import { compose } from '@wordpress/compose';
// Show and Dispatch the discover
const NewNotices = ({ notices, removeNotice }) => {
//Uncaught TypeError: notices is undefined
const snackbarNotices = notices.filter((discover) => discover.kind === 'snackbar');
return (
<>
<SnackbarList
className="cwg-admin-notices"
notices={snackbarNotices}
onRemove={removeNotice}
/>
</>
);
}
export default compose([
withSelect((select) => ({
notices: select('core/notices').getNotices(),
})),
withDispatch((dispatch) => ({
removeNotice: dispatch('core/notices').removeNotice,
})),
])(NewNotices);
<>
//Create the discover on btn click on
<Button
isPrimary
onClick={() =>
{
settings.save();
dispatch('core/notices')
.createNotice(
'success',
__('Settings Saved', 'slug'),
{
kind: 'snackbar',
isDismissible: true,
icon:
<Icon icon="smiley" />
}
);
}}
>
{__('Save', 'slug')}
</Button>
<NewNotices />
</>