Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update README.md for @wordpress/element. createRoot not available until WordPress 6.2 #49309

Merged
merged 13 commits into from
Mar 27, 2023

Conversation

helgatheviking
Copy link
Contributor

Mention that createRoot() is not available until React 18/WordPress 6.2

What?

Adding version-specific information about createRoot and an example for mounting an app that works in both WP 6.1 and 6.2.

Why?

This PR is necessary, because I spent an entire day wondering why createRoot was undefined despite following the example exactly. This is not available prior to React 18/WordPress 6.2 and many plugins/themes will need to support both versions.

How?

Explain the version limitations

Testing Instructions

Might warrant another PR because you can't actually drop the example into a WP template to test since the wp script isn't loaded until the footer (or so it appears to me since I was getting wp undefined errors).

To test, I inserted <div id="greeting"> in a template and then added the script inline:

/**
 * Mount example component
 */
function test_react_mount() {
	
    wp_add_inline_script( 'wp-element', '
				
        function Greeting( props ) {
		return wp.element.createElement(
			"span",
			null,
			"Hello " + props.toWhom + "!"
		);
	}

        if ( wp.element.createRoot ) {
        
	        wp.element
		        .createRoot( document.getElementById( "greeting" ) )
		        .render( wp.element.createElement( Greeting, { toWhom: "World" } ) );
        
        
        } else {
	        wp.element
		        .render( wp.element.createElement( Greeting, { toWhom: "World" } ), document.getElementById( "greeting" ) );
        }
	    ' );
}
add_action( 'wp_enqueue_scripts', 'test_react_mount', 99 );

Mention that `createRoot()` is not available until React 18/WordPress 6.2
@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Mar 23, 2023
@github-actions
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @helgatheviking! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@helgatheviking helgatheviking changed the title Update README.md Update README.md for wordpress/element. createRoot not available until WordPress 6.2 Mar 23, 2023
@aurooba
Copy link
Member

aurooba commented Mar 23, 2023

This is lovely, I echo @helgatheviking! It could also be worth adding JSX example, like the one @ryanwelcher shared on Twitter:

image

@helgatheviking
Copy link
Contributor Author

Love that! I might even suggest not using <App /> as that will throw an error since App is not imported. If you're familiar with react you might recognize this, but it would be nice for newbies (ie: me) if you had something that actually worked right away.... like <h1>Hello World</h1>

@skorasaurus skorasaurus added the [Type] Developer Documentation Documentation for developers label Mar 23, 2023
@margolisj
Copy link
Contributor

I also ran into this issue trying to update a script with createRoot for rendering a component via the add_menu_page api. So my understanding of this is that there are different versions of gutenberg-esque libraries floating around in Wordpress. The Gutenberg plugin version in the Page + Post editor (if installed) and WP Core version (accessibly everywhere else like the FSE or admin panel). Does anyone know if that understanding is correct?

@skorasaurus
Copy link
Member

I also ran into this issue trying to update a script with createRoot for rendering a component via the add_menu_page api. So my understanding of this is that there are different versions of gutenberg-esque libraries floating around in Wordpress. The Gutenberg plugin version in the Page + Post editor (if installed) and WP Core version (accessibly everywhere else like the FSE or admin panel). Does anyone know if that understanding is correct?

If you have the Gutenberg plugin activated in your installation of wordpress, the page/post editor (e.g. https://yourwebsite.test/wp-admin/post.php?post=63&action=edit) and the site editor (e.g. yourwebsite.test/wp-admin/site-editor.php?) will have the newest published releases of gutenberg and https://make.wordpress.org/core/tag/gutenberg-new/ can keep you up to date if you wish to follow what's in the latest releases of gutenberg.

If you don't have the gutenberg plugin activated; you'll still have access to the site editor and page/post editor, it just won't be the newest version.

@helgatheviking helgatheviking changed the title Update README.md for wordpress/element. createRoot not available until WordPress 6.2 Update README.md for @wordpress/element. createRoot not available until WordPress 6.2 Mar 23, 2023
extra common elements for clarity
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice revision to the documentation 👍

packages/element/README.md Outdated Show resolved Hide resolved
packages/element/README.md Outdated Show resolved Hide resolved
@gziolo gziolo added the [Package] Element /packages/element label Mar 25, 2023
helgatheviking and others added 6 commits March 26, 2023 14:58
Add "@SInCE 6.2.0 Introduced in WordPress core." comment.
Mark changes related to React 18 as new and deprecated.
Apply changes from the `npm run docs:build` run.
Revert changes for `createInterpolateElement`.
Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied changes to JSDoc comments to reflect changes between React 18 and 17.

@jsnajdr, is there anything else we should mark as new or deprecated in WP 6.2.0?

packages/element/README.md Outdated Show resolved Hide resolved
packages/element/README.md Outdated Show resolved Hide resolved
Trying again with GitHub UI.
packages/element/README.md Outdated Show resolved Hide resolved
packages/element/README.md Outdated Show resolved Hide resolved
Try again making CI pass 😅
@gziolo
Copy link
Member

gziolo commented Mar 27, 2023

All good with CI, all checks satisfied 🎉

Thank you so much for opening this PR.

@helgatheviking
Copy link
Contributor Author

@gziolo Thanks for cleaning it up!

@gziolo gziolo merged commit 74d1b0d into WordPress:trunk Mar 27, 2023
@github-actions github-actions bot added this to the Gutenberg 15.5 milestone Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Element /packages/element [Type] Developer Documentation Documentation for developers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants