-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Reconsider @wordpress/element as a React wrapper and use React more directly. #54074
Comments
While generally, I'm supportive of this change, I think we have to be careful about stating this won't have any impact on WordPress (although I think I understand what you meant by this). There will be impact on:
This can all be mitigated to a degree but I think it's still worth acknowledging that impact. |
@nerrad Good point about the documentation changes / tutorials etc... Just noting that regardless of whether they compile to wp.element or React. Scripts will continue to work for the foreseeable future. |
I agree with this change too. I'm not 100% aware of all of the original arguments, but at this point, if the project were to switch to a different library (which is unlikely IMO), it'd still require large breaking changes to For the sake of argument, say we switched to a different library. We'd still need to keep React support for some time for backwards compatibility. As a result, the new framework functions would need to be under a different namespace in So it'd need to be exposed under a different name (and probably a different package or namespace within So I agree that |
We'll still have to maintain the The way I see it, the pro of simple package consumption doesn't really benefit the WP while introducing more work for maintainers. P.S. I generally agree with the proposal; we just need to be sure that the pros outweigh the cons. |
I'm not sure I understand what you mean when you say "more work". Can you clarify? |
Thanks for the proposal @youknowriad! I agree with it, and I must say this is something we've already been considering for various reasons, including:
There is the downside of having to maintain the element package to maintain backward compatibility, but we can totally deprecate the package and even stop maintaining it. By using an old I realize that removing |
Sorry for the late reply, @youknowriad 🙇
Maintaining the package for BC, updating documentation, and swapping imports in the core. |
The longer I think about this the more I think it's a bad idea but throwing it out there anyways. Would it make sense to use the I know that I for example have shipped a lot of usages of Also when in the context of WordPress I personally quite like the mental modal of I know this already isn't the case and things like But it does seem more "strange" to import something that you don't have installed on the project that isn't namespace as a WordPress dependency. I very much understand the benefits for the project as a whole though :) |
Can I ask what is the reason for this change, what is gained by doing this? We exclusively reference @wordpress/element not React in our work because thats what it is. You can't take our components and pop them into any React app because they're not meant for that, they're meant to be run under a WordPress React environment driven by @wordpress/element. |
Does this change mean that methods imported from This is, these calls...
will need to be transformed into the following?
Or will the |
Abstraction is ALWAYS important when developing a framework like the new WordPress. Coupling to a particular library (i.e., React) makes the framework dependent on changes in that ecosystem. Where wordpress/element is dependent on React, THAT should be abstracted away, not more-tightly integrated. |
Thanks for the feedback @fabiankaegy @sethrubenstein @juanmaguitar and @mikeritter let me clarify a few things: First let's make it clear, this change doesn't impact WordPress developers at all:
Now, that this is out of the way, let me try to address your points above:
I don't think it makes sense to remap automatically, because the packages are not strictly equivalent, we expose most of React APIs in @wordpress/element but we also expose more APIs.
I understand but also as you said, wordpress ships "react", "lodash", "tinymce", "moment"... so that point don't really hold globally IMO. you can also "import" a number of these dependencies when using wp-scripts for a long time now. There's no change.
The reason is explained above, it's not targeted for WordPress developers, it's targeted for users of the @wordpress/* packages in any application, whether it's a WordPress headless JS frontend or any JS app really. These developers usually rely on JSX being transformed in React in their build tools (it's the default for most build tools) and they use react libraries, so they expect that any react library work. So it's both a clarification and a DevX improvement for these users. You can read this issue #53874 to have an idea about this kind of users and their existing struggles.
No, no change is needed but IMO, we should recommend using "react" directly because both of them are exposed already and the abstraction doesn't really bring any value.
Oh I agree that abstraction is necessary in development but the reality as I explained above, is that this is not really an abstraction. It's what we call a Also, a lot of people do use the underlying framework, if you've built a plugin using these scripts already, you might have used an external react library like "radix", "react-redux" or any React library really. Developers expect these libraries to work by default even if they're using I hope this clarifies a few things. |
A very similar question about API design was recently raised by @luisherranz when working on the Interactivity API. The Interactivity API uses Preact under the hood, exposes its API to WordPress developers, and also adds its own custom APIs. Now the question is, should we import them all from one import { useEffect, useInit, useWatch } from "@wordpress/interactivity"; or to import the native Preact stuff from the import { useEffect } from "preact";
import { useInit, useWatch } from "@wordpress/interactivity"; I think there are good reasons for both approaches, and the answer depends on who you are and what is your point of view. You can be a "WordPress developer," with your view centered around the WordPress platform, and all things React are just little extensions of this platform. Then it's natural to have all the platform APIs named with the But you can also be a "React developer," who works on React projects, has knowledge about React itself, and is trying to apply and reuse this knowledge on a WordPress project. React, just like WordPress, is also a huge ecosystem with a community, documentation, code examples, Stack Overflow questions, blog posts, third party libraries and tooling. By trying to abstract React away and pretending everything is just By importing directly from For these reasons, it makes sense to me to continue exporting React and Preact API under both naming conventions: |
The docs and wp-scripts package have both been updated. I think we can close this issue right now. We still have a lot of code in Gutenberg that uses |
When initially evaluating frameworks, we built the @wordpress/element package as a wrapper around React to potentially support more framework or account for breaking changes. I think it's time for us to evaluate that decisions.
Most of our packages that are React libraries (compose, components...) do the following today:
I believe that this is a contradiction and is not great for multiple reasons:
Note that we do have some functions in @wordpress/element that are additional like
renderToString
orcreateInterpolateElement
...Also let's note that any potential breaking change that happens in React in the future will be automatically a breaking change in WordPress as well because "React" is also a public API in WordPress through the "react" script handle. In other words, wordpress/element's abstraction doesn't allow us to prevent future potential React breaking changes.
Proposal
For these reasons, I'd like to propose that we simplify things a little bit (which will also make it easier for third-party developpers to consume our packages):
Just noting as well that such a change won't have any impact on WordPress. It won't introduce any breaking changes to WordPress. It will introduce what can be seen as a breaking change for the
wp-scripts
dependency as it will switch from generatingwp.element.createElement
toReact.createElement
... but since it's a dev tool that is upgraded manually by developers, this is acceptable.The pros of this change is that it will make consuming @WordPress packages through npm way simpler for any third-party JS application.
cc @WordPress/gutenberg-core @jsnajdr @gziolo
The text was updated successfully, but these errors were encountered: