The block code is as such:
wp.blocks.registerBlockType('myblocks/gallery', {
title: 'Gallery',
icon: 'my-gallery',
category: 'common',
attributes: {
},
edit: props => {
const [selectedFiles, setSelectedFiles] = useState([]);
const [errorMessage, setErrorMessage] = useState('');
const handleFiles = (files) => {
for (let i = 0; i < files.length; i++) {
if (validateFile(files[i])) {
} else {
//the code goes on...
It of course produces useState is not defined
error, but I've no idea how to import it. When I attempt const {useState} = require( '@wordpress/element' );
, I get a referenceError: require is not defined
; and I don't know where to place import { useState } from '@wordpress/element';
I'm probably misunderstanding the fundamental structure, since I'm new to Wordpress development, but I've no idea where to go from .
return <p className={ classesVariable }>{ variable}</p>
orimport { useState } from
@wordpress/element';` won't work. This isn't necessarily a WordPress thing but a modern JS/React thing. Also if you want a custom gallery, it's much easier to reuse the existing gallery with a block variant than to rebuild the entire thing from scratch as a brand new block, likewise if your gallery just looks different a block style will do the job just as well if not better