Replies: 5 comments 4 replies
-
As discussed with you, I think this proposal and the reasoning make sense but let's bring some more eyes on this proposal. @WordPress/gutenberg-core Noting that ideally this is done in a single commit that we can add to the |
Beta Was this translation helpful? Give feedback.
-
Fully agreed. If we ever need to backtrack, an import map could achieve the same result. |
Beta Was this translation helpful? Give feedback.
-
Fully agree and embrace React! |
Beta Was this translation helpful? Give feedback.
-
I want to note that the Interactivity API uses Preact, so keeping However, this possibility seems very minuscule to me, and importing React modules directly has clear benefits so I'm also in favor of importing |
Beta Was this translation helpful? Give feedback.
-
I agree with the proposal. It might be worth sharing in the |
Beta Was this translation helpful? Give feedback.
-
Background
For a long time, we've been importing any
react
andreact-dom
utilities and functions from the@wordpress/element
, which essentially re-exports them.Back in the day, we introduced
@wordpress/element
as an abstraction layer, which could theoretically allow us to use a different technology under the hood if needed at some point.However, a few years later, it's easy to see that React is a first-class citizen in the Gutenberg codebase, and we can't just remove it because everything depends on the intricate way React works under the hood.
That being said,
@wordpress/element
feels more and more like an unnecessary wrapper that only adds a redundant abstraction layer. On the contrary - in some 3rd party projects that rely on@wordpress
packages, it's caused trouble since it required keepingreact
andreact-dom
versions up to date with the underlying versions in@wordpress/element
in order to prevent duplication.Proposal
I'd like to propose that we start importing
react
directly in the Gutenberg codebase. Essentially, this means we'll migrate all existing usages in the Gutenberg codebase. It also means we'll inverse this existing ESLint rule:so across Gutenberg, we will not be allowed to import from
@wordpress/element
, but will rather import modules directly fromreact
andreact-dom
.At that point, we'll be able to soft-deprecate
@wordpress/element
, encouraging 3rd party developers to also import fromreact
andreact-dom
.Of course, for backward compatibility, for the long-term we'll continue to maintain
@wordpress/element
which will still re-export the React utilities, and will continue to allow 3rd party plugins to use the React APIs in wp-admin.Rationale
At this point you're probably asking why we want to do it, and why NOW. Let's dive into that.
Previously, we didn't really have a good motivation to move forward with this project-wide change. However, with the upcoming React 19, we might have to do it.
React Compiler, an essential React 19 feature, will not recognize any
react
/react-dom
modules that are re-exported. That means that some of the React Compiler rules can't really be followed. For example, React Compiler won't know thatuseRef()
from@wordpress/element
is the one coming fromreact
, and as a result it won't be able to treat it accordingly with the expected special exceptions.We'd like to leverage React Compiler for Gutenberg, as it promises to optimize the app at build time and promises to have a positive impact on runtime performance. We're experimenting with React Compiler in #61788 and are incrementally addressing all the blockers, aiming to enable it as soon as it's considered ready for production.
I've been exploring whether it's possible to extend the React Compiler to recognize external modules as known ones, but the chance of actually having support for that is low. See facebook/react#31138 for more information.
Luckily, this won't be a problem if we use
react
andreact-dom
directly.Proposed approach
Since changing all
@wordpress/element
imports is a repository-wide change that will affect everyone's code, I'd suggest that we:Feedback
I'd love to hear everyone's feedback and hear if anyone has concerns or ideas to consider in the meantime.
Thanks, everyone!
Beta Was this translation helpful? Give feedback.
All reactions