I recently started working with custom blocks and I have created a simple static custom block. Block setup works and block can be added to the block editor. But when I save it and preview it, the block is not shown and the block editor cannot be reloaded due to an error. Editor completely broken after this showing a white screen. How we use Button component inside save component? (I use this with theming so I can get the button theme setting for the frontend.).
import { registerBlockType } from '@wordpress/blocks';
import { Button } from '@wordpress/components';
registerBlockType('mynamespace/buttontest', {
title: 'mynamespace/buttontest',
edit: () => {
return (
<Button>Test</Button>
);
},
save: () => {
return (
<Button>Test2</Button>
);
}
})
This yields following errors when I saved.
react-dom.min.js?ver=18.2.0:10 Error: Minified React error #300; visit https://reactjs.org/docs/error-decoder.html?invariant=300 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
at wt (react-dom.min.js?ver=18.2.0:10:47841)
at vr (react-dom.min.js?ver=18.2.0:10:56452)
at js (react-dom.min.js?ver=18.2.0:10:121453)
at wl (react-dom.min.js?ver=18.2.0:10:88659)
at bl (react-dom.min.js?ver=18.2.0:10:88587)
at yl (react-dom.min.js?ver=18.2.0:10:88450)
at il (react-dom.min.js?ver=18.2.0:10:85274)
at fl (react-dom.min.js?ver=18.2.0:10:85661)
at Nn (react-dom.min.js?ver=18.2.0:10:32474)
at react-dom.min.js?ver=18.2.0:10:83226
When I refresh the editor, the content is not saved and following error pops up.
blocks.min.js?ver=7204d43123223474471a:10 Block validation: Block validation failed for `mynamespace/buttontest` (Object).
Content generated by `save` function:
<div role="group" class="components-button-group"><button type="button" class="components-button">Test2</button></div>
Content retrieved from post body:
(anonymous) @ blocks.min.js?ver=7204d43123223474471a:10
and
Error: Switching useSelect from mapping to mapping is not allowed
at Qe (data.min.js?ver=dc5f255634f3da29c8d5:9:2838)
at Ys (editor.min.js?ver=3f5791ae786456067a27:12:140050)
at editor.min.js?ver=3f5791ae786456067a27:12:142736
at wt (react-dom.min.js?ver=18.2.0:10:47637)
at vr (react-dom.min.js?ver=18.2.0:10:56452)
at js (react-dom.min.js?ver=18.2.0:10:121453)
at wl (react-dom.min.js?ver=18.2.0:10:88659)
at bl (react-dom.min.js?ver=18.2.0:10:88587)
at yl (react-dom.min.js?ver=18.2.0:10:88450)
at il (react-dom.min.js?ver=18.2.0:10:85274)