-
I want to create a Gutenberg block that uses React on the client rendered page (not the admin pages).
EDIT: I also asked the question on Stack Overflow, and have been told it's not possible. Is that true? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@johnhooks This might be useful https://stackoverflow.com/questions/51771125/wordpress-gutenberg-react-components-on-front-end |
Beta Was this translation helpful? Give feedback.
-
I'm still working out the details, but the best solution I've found so far is to add add_action( 'wp_enqueue_scripts', 'my_enqueue_plugin_js' ); // Loads on frontend
function my_enqueue_plugin_js() {
wp_enqueue_script(
'my-plugin-frontend',
plugin_dir_url( __FILE__ ) . 'js/plugin.js',
['wp-element']
);
} |
Beta Was this translation helpful? Give feedback.
@tomdevisser
I'm still working out the details, but the best solution I've found so far is to add
wp-element
as a dependency of my enqueued script. When built with@wordpress/scripts
it adds the Gutenberg version of React to the frontend under the globalwp.react
and transforms the JSX to usewp.react.createElement
. It works, but I haven't confirmed this is the standard way to do this.