JSX onClick listener in custom static block plugin
-
I’m creating a static block plugin where I need to define action when user clicks on button, however onClick JSX attriibute does not work for me.
This is my code in save.js that should be in preview part – not in editor:
export default function save({attributes}) { const blockProps = useBlockProps.save(); const onBlockClick = () => { document.getElementsByClassName(blockClass)[0].classList.add("blue"); console.log("clicked"); }; return ( <div { ...blockProps } className={"exe-more-info-button "+attributes.className} onClick={onBlockClick}> <span className="wp-block-button__link wp-element-button"> <RichText.Content tagName="p" value={ attributes.content } /> </span> </div> ); }
It should put another class to class list of specified element, and log something to console (for this specific debug reason), but it doesn’t.
What I was able to find online is that the function that onClick refers to in jsx must be in
const <name> = () => {}
format, but it does not work with it too.The thing is that it behaves like there wasn’t any onClick handler – no console output, no error, nothing… Even in Dev Tools in browser there is no onclick in html in that particular div.
What am I doing wrong, and how can I define onClick handler correctly?
WP 6.2.2
PHP 8.2.4Thanks in advance.
- The topic ‘JSX onClick listener in custom static block plugin’ is closed to new replies.