I want to create a Gutenberg block that uses React on the client rendered page (not the admin pages).
Is React code split from the rest of Gutenberg?
If so, how can I enqueue that specific bundled React to be used?
I found it, wp-element. The @wordpress/scripts
should handle the heavy lifting of transforming the JSX in the proper way.
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']
);
}
Once we do this we will have window.wp.element available in our JavaScript. This contains the ReactDOM render() function as well as createElement() if you wanted to write React without JSX.
import ... from 'react';
in a bundle intended for the frontend
data
attributes in your saved markup