admin管理员组文章数量:1122832
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 .
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 .
Share Improve this question asked May 4, 2023 at 16:51 budgiebeaksbudgiebeaks 1114 bronze badges 4 |1 Answer
Reset to default 0Okay, just lucked upon an answer. Added
const {
element: {
useState,
},
} = wp;
at the top of the file, it seemingly works, though I still have no idea what is going on.
本文标签: How can I include React useState in a custom gutenberg block plugin
版权声明:本文标题:How can I include React useState in a custom gutenberg block plugin? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289949a1928411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 – Tom J Nowell ♦ Commented May 4, 2023 at 17:38