admin管理员组文章数量:1287633
I am trying to use withSelect and withDispatch to display an admin notice when "save" button is clicked. I am using the following code from this repo but it throws an error: "notices is undefined". Here is the code I am using:
import { Icon, Button, SnackbarList } from '@wordpress/components';
import { dispatch, withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
// Display and Dispatch the notice
const NewNotices = ({ notices, removeNotice }) => {
//Uncaught TypeError: notices is undefined
const snackbarNotices = notices.filter((notice) => notice.type === '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 notice on btn click
<Button
isPrimary
onClick={() =>
{
settings.save();
dispatch('core/notices')
.createNotice(
'success',
__('Settings Saved', 'slug'),
{
type: 'snackbar',
isDismissible: true,
icon:
<Icon icon="smiley" />
}
);
}}
>
{__('Save', 'slug')}
</Button>
<NewNotices />
</>
I am trying to use withSelect and withDispatch to display an admin notice when "save" button is clicked. I am using the following code from this repo but it throws an error: "notices is undefined". Here is the code I am using:
import { Icon, Button, SnackbarList } from '@wordpress/components';
import { dispatch, withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
// Display and Dispatch the notice
const NewNotices = ({ notices, removeNotice }) => {
//Uncaught TypeError: notices is undefined
const snackbarNotices = notices.filter((notice) => notice.type === '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 notice on btn click
<Button
isPrimary
onClick={() =>
{
settings.save();
dispatch('core/notices')
.createNotice(
'success',
__('Settings Saved', 'slug'),
{
type: 'snackbar',
isDismissible: true,
icon:
<Icon icon="smiley" />
}
);
}}
>
{__('Save', 'slug')}
</Button>
<NewNotices />
</>
Share
Improve this question
edited Sep 26, 2021 at 21:17
Badan
asked Sep 26, 2021 at 20:34
BadanBadan
2251 silver badge7 bronze badges
2
|
1 Answer
Reset to default 0I figured it out. The component was not rerendering, when clicking the "Save" button. I included the compose
component in the render method and all is working good now:
const Notices = ({ notices, removeNotice }) => {
const snackbarNotices = notices ? notices.filter((notice) => notice.type === 'snackbar') : [];
return (
<>
<SnackbarList
className="cwg-admin-notices"
notices={snackbarNotices}
onRemove={removeNotice}
/>
</>
);
}
const AppNotices = compose(
withSelect((select) => ({
notices: select('core/notices').getNotices(),
})),
withDispatch((dispatch) => ({
removeNotice: dispatch('core/notices').removeNotice,
})),
)(Notices);
--
<Button
isPrimary
onClick={() =>
{
settings.save();
dispatch('core/notices')
.createNotice(
'success',
__('Settings Saved', 'slug'),
{
type: 'snackbar',
isDismissible: true,
icon: <Icon icon="smiley" />
}
);
}}
>
{__('Save', 'slug')}
</Button>
<AppNotices/>
本文标签: rest apiCannot DIsplay a Snackbar Notice on Button ClickNotice is undefined
版权声明:本文标题:rest api - Cannot DIsplay a Snackbar Notice on Button Click - Notice is undefined 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741298665a2370961.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
const snackbarNotices
. Apparently,notices
param in the<NewNotices/>
component is undefined (when i try to console.log it, it is undefined.) – Badan Commented Sep 27, 2021 at 6:47