Skip to content

What is the canonical way to use the version of React that ship with Gutenberg on the front end? #48446

Answered by johnhooks
johnhooks asked this question in Q&A
Discussion options

You must be logged in to vote

@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 global wp.react and transforms the JSX to use wp.react.createElement. It works, but I haven't confirmed this is the standard way to do this.

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']
    );
}

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@tomdevisser
Comment options

@johnhooks
Comment options

Answer selected by tomdevisser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants