-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Including version
in @wordpress/element
#49308
Comments
You can also check if the new render method is defined and swap logic based on that. /**
* Grab everything from @wordpress/element with a wildcard import
*/
import * as Element from "@wordpress/element";
/**
* Internal Dependencies
*/
import App from "./app";
/**
* If version is less than 18 use `render` to render the app
* otherwise use `createRoot` to render the app
*/
if ( Element.createRoot === undefined ) {
Element.render(<App />, document.getElementById("root"));
} else {
const domNode = document.getElementById("root");
const root = Element.createRoot(domNode);
root.render(<App />);
} cc @jsnajdr |
I was using render in multiple files, so I dealt with it by combining them as a single function. file name: import { render, createRoot } from '@wordpress/element';
export default function (root, component) {
if (undefined !== createRoot) {
createRoot(root).render(component);
} else {
render(component, root);
}
} Examples of use: // import compatible-render.js
import compatibleRender from './path-to/compatible-render';
/* ... other code ... */
compatibleRender( document.getElementById("root"), <App /> ); |
The solutions are great! And checking for undefined imports is definitely an excellent way to deal with the render issue specifically. But there are definitely going to be gotchas as we support both React 17 and 18 for a little bit, so I'm still in favour of including version as per @dgwyer's PR (#49312) into |
What problem does this address?
With WordPress 6.2, React 18 will come bundled with WordPress, and for the time being a lot of us will have to support both React 17 and 18 in our themes and plugins. It would be really nice if
version
from React came bundled inside@wordpress/element
for an easier verbose check when mounting your React app.Without
version
coming fromelement
:What is your proposed solution?
Include
version
in the import from React in@wordpress/element
Props to @ryanwelcher and @dgwyer for helping me think through this on Twitter :)
The text was updated successfully, but these errors were encountered: