2

Within my custom block plugin, I am using create-block to generate my block environment for block editing. Within my `src/edit.js file I have the following setup...

import { __ } from '@wordpress/i18n';
import {
    useBlockProps,
    Fragment,
    RichText,
    BlockControls,
} from '@wordpress/block-editor';
import './editor.scss';

export default function Edit( { attributes, setAttributes } ) {
    const { text } = attributes;

    // additional options for our RichText component - https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/rich-text/README.md
    return (
        <Fragment>
            <BlockControls controls={[{
                title: "Button 1",
                icon: "admin-generic",
                onClick : () => console.log("Button 1 clicked!"),
            },]} />
            <RichText
                { ...useBlockProps() }
                onChange={ ( value ) => setAttributes( { text: value } ) }
                value={ text }
                placeholder={ __( 'My chocolate placeholder', 'chocolate' ) }
                tagName="h4"
                allowedFormats={ 'core/bold' }
            />
        </Fragment>
    );
}

...But when I run npm start, and go to my edit page, I get the following error in the console...

Block validation: Block validation failed for `course-blocks/chocolate` ( ....}

My save.js file looks like so...


import { useBlockProps, RichText } from '@wordpress/block-editor';

export default function save( { attributes } ) {
    const { text } = attributes;
    return (
        <RichText.Content
            { ...useBlockProps.save() }
            tagName="h4"
            value={ text }
        />
    );
}

and my attributes are being pulled from my block.json...

{
    "$schema": "https://schemas.wp.org/trunk/block.json",
    "apiVersion": 2,
    "name": "course-blocks/chocolate",
    "version": "0.1.0",
    "title": "Chocolate",
    "category": "text",
    "description": "A playground for block development.",
    "supports": {
        "html": false
    },
    "textdomain": "chocolate",
    "editorScript": "file:./index.js",
    "editorStyle": "file:./index.css",
    "style": "file:./style-index.css",
    "attributes": {
        "text": {
            "type": "string",
            "source": "html",
            "selector": "h4"
        }
    }
}


5
  • 1
    I don't see any problem with your use of Fragment. It mimics how the core heading block works: github.com/WordPress/gutenberg/blob/trunk/packages/… The main difference I see is that the block props are added as the last properties, not the first. But that might not even be the issue. Block validation is usually a problem with the saved markup. What does your save method look like? Commented Mar 14, 2023 at 16:40
  • @klewis this has nothing to do with how to use Fragment in an edit component, it's the save component that gets used for block validation, and since there is no save component in your question there's no way to solve your problem with what you shared! Please edit the save component into your question and update the title to ask about block validation not fragments, otherwise you will never be able to solve this problem with the question you asked.
    – Tom J Nowell
    Commented Mar 14, 2023 at 17:29
  • 1
    and console.log is not the cause of this either, again block validation is all about the save component, not the edit component
    – Tom J Nowell
    Commented Mar 14, 2023 at 17:32
  • 1
    @klewis you did not include the save component! And your answer is not the solution, you may have accidentally fixed the problem indirectly by addressing linter rules but the linter rules themselves can't cause block validation problems
    – Tom J Nowell
    Commented Mar 14, 2023 at 18:53
  • 1
    @klewis block validation is when the editor takes the blocks saved output, then runs the block attributes through the save component. If the save components output and the existing output match then it's valid. If they differ then something has gone wrong and block validation has failed. The same attributes should always generate the same output.
    – Tom J Nowell
    Commented Mar 14, 2023 at 19:43

1 Answer 1

1

The Fragment export lives in the @wordpress/element package, not block-editor.

Having said that, you no longer have to explicitly write <Fragment> (as of React 16.2). Instead, you can just use the "empty tag":

return (
    <>
        <p>One</p>
        <p>Two</p>
    </>
);
1
  • 1
    thanks for improving my education fragments for blocks.
    – klewis
    Commented Mar 14, 2023 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.