Trying to turn the Edit function into a class to be able to use React lifecycle methods #36929
-
In order to be able to use React lifecycle methods I'm trying to change the default 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';
import icons from "../../icons";
import {Component} from "@wordpress/element";
import {useBlockProps} from "@wordpress/block-editor";
export default class TestClass extends Component {
render() {
return (
<div {...useBlockProps()}>
This is only a test!!
</div>
);
}
}
/**
* Every block starts by registering a new block type definition.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/
*/
registerBlockType('multiple-blocks-plugin/notice', {
icon: icons.note,
/**
* @see ./edit.js
*/
edit: TestClass, //Edit,
/**
* @see ./save.js
*/
save,
}); That's an approach I've seen in some tutorials like this one but, for some reason, I'm not been able to do it. The above code, gives me the following error at the browser console:
So now I have the following questions:
Note: Digging for a solution, I've found that the problem is caused by the function |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It turned out that, at least for my specific use case, there was no need to use a class or interact with React lifecycle methods. My code was already working with the component as a function but for some reason I wasn't able to see it in that moment. But I've made some research an discovered that the guys at React have an argument against using class in javascript. To address things that could just be done through classes in the past they have created the hooks functionality to be used by functional React components. Digging for a solution, I've found out that the problem is caused by the function useBlockProps. Is there a way to avoid it without loosing the useBlockProps provided functionalities? At the present moment it seems to be no way to use classes and still have the useBlockProps functionality since it seems not to be implemented for classes by the folks from Wordpress and React Hooks doesn't work with classes. Am I doing something wrong? Yes, I had no need to use a class nor to direclty interact with React lifecycle methods since my functional code was already working. Is it not allowed anymore?
Is there a better alternative to be able to use the React lifecycle? Yes, it seems that it can be done now from a functional component with the |
Beta Was this translation helpful? Give feedback.
It turned out that, at least for my specific use case, there was no need to use a class or interact with React lifecycle methods. My code was already working with the component as a function but for some reason I wasn't able to see it in that moment.
I have probably made a mistake while testing.
But I've made some research an discovered that the guys at React have an argument against using class in javascript. To address things that could just be done through classes in the past they have created the hooks functionality to be used by functional React components.
If you have the need to interact with the React state, take a look at the new state hook as everything seems to be functional no…