Fixed: Console.log twice in the edit function
Hi Guys I am using $ npx @wordpress/create-block todo-list
package and I found something interesting. I have not changed everything in the file structure but my console.log or any variable get outputted twice.
How to reproduce this problem?
- Installing the @wordpress/create-block package
- Opening edit.js and adding
console.log(test)
above the render
My index and edit.js files
/**
* Registers a new block provided a unique name and an object defining its behavior.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
*/
import { registerBlockType } from '@wordpress/blocks';
/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* All files containing `style` keyword are bundled together. The code used
* gets applied both to the front of your site and to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './style.scss';
/**
* Internal dependencies
*/
import Edit from './edit';
import save from './save';
/**
* Every block starts by registering a new block type definition.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
*/
registerBlockType( 'create-block/beta-toolbox', {
/**
* @see ./edit.js
*/
edit: Edit,
attributes: {
// New code added. Comment clarity
imagedata: {
type: 'array',
default: [{image: link1},{image: link1},{image: link1}]
}
},
/**
* @see ./save.js
*/
save,
} );
And the edit.js file
/**
* Retrieves the translation of text.
*
* @see https://developer.wordpress.org/block-editor/packages/packages-i18n/
*/
import { __ } from '@wordpress/i18n';
/**
* React hook that is used to mark the block wrapper element.
* It provides all the necessary props like the class name.
*
* @see https://developer.wordpress.org/block-editor/packages/packages-block-editor/#useBlockProps
*/
import { useBlockProps } from '@wordpress/block-editor';
/**
* Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
* Those files can contain any CSS code that gets applied to the editor.
*
* @see https://www.npmjs.com/package/@wordpress/scripts#using-css
*/
import './editor.scss';
/**
* The edit function describes the structure of your block in the context of the
* editor. This represents what the editor will render when the block is used.
*
* @see https://developer.wordpress.org/block-editor/developers/block-api/block-edit-save/#edit
*
* @return {WPElement} Element to render.
*/
import {useEffect } from @wordpress/element
export default function Edit(props) {
console.log(props.attributes.imagedata);
// pushing the a new element to this array results to have adding it twice
return (
p { ...useBlockProps() }
{ __( 'Beta Toolbox – hello from the editor!', 'beta-toolbox' ) }
/p
);
}
Edit: By wrapping the code in an useEffect() hook it is solved
Topic block-editor bug plugin-development plugins Wordpress javascript
Category Web