Skip to content

Reconsider @wordpress/element as a React wrapper and use React more directly. #54074

@youknowriad

Description

@youknowriad

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:

  • Define React as a peer dependency
  • Ship files that use createElement from @wordpress/element instead of React

I believe that this is a contradiction and is not great for multiple reasons:

  • It's not clear whether our packages should be considered "React" libraries or "@wordpress/element" libraries.
  • It's not really clear what the role of @wordpress/element is is anymore because we expose almost all the APIs of React through the package without any change or adaptation.

Note that we do have some functions in @wordpress/element that are additional like renderToString or createInterpolateElement...

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):

  • I'd like for our packages to become simple "React libraries", in other words they'd ship React.createElement instead of createElement from @wordpress/element
  • Keep the peer dependencies as they are (aka React)
  • If a package needs an extra function that is provided by @wordpress/element they can import it from there like any regular library.
  • remove any special JSX bundling config we have (just rely on the community tools for JSX)
  • In other words, we stop pretending like @wordpress/element is a wrapper around React.

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 generating wp.element.createElement to React.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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions