1

I try to make a Gutenberg plugin but I have problem to set a value of attributes. I npm this template npx @wordpress/create-block example-plugin --template @ryanwelcher/multiple-blocks-template because I want create a multiblock plugin. the problem is where and how register attributes.

I register it on block.json:

 "attributes": {
        "content": {
            "type": "string",
            "selector": "h2"
        }
    },

Then I used it on edit.js

 export default function Edit({attributes: {content}, setAttribute}) {
      const blockProps = useBlockProps();

     return (
      
         <p {...useBlockProps()}>
             <input value={content} />
               <TextControl label="Question:" value={content}
                allowedFormats={["core/italic"]}
                onChange={x => setAttributes({content: x})} >
             {__('larum ipsume', 'page-builder')}
         </p>
     );
 }

this method work but I have an error. I cannot write all sentence but just first letter and it refresh my input.

2
  • I have the same problem... Everything is work but it doesnt save the attributes! I think it must be some import Commented Apr 5, 2022 at 20:35
  • 1
    The function you are deconstructing from Edit()'s arguments is missing an s - you should be receiving an error in the JS console detailing that setAttributes is not defined. Additionally, with no source argument specified in your block.json configuration the attribute should be stored in an HTML comment, but you've specified a selector argument which might be causing an unexpected behavior. Try correcting the typo and removing "selector": "h2" from your blocks.json file.
    – bosco
    Commented Apr 25, 2022 at 21:27

0

Your Answer

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

Browse other questions tagged or ask your own question.