Intercept and modify JSX at runtime

The underlying jsx-async-runtime lets you intercept the rendering of JSX elements, so you can modify or replace existing markup.

Every JSX element is turned into HTML by calling this.jsxToString. A component can create a child context with Object.create(this), assign its own jsxToString there and render its children with it. The override sees every element before it becomes HTML, so it can change props, swap tags or drop elements entirely. Because the override only lives on the child context, everything rendered outside of the component stays untouched.

In this example, a HouseKeeper component wraps a weather widget from a third-party library. The widget ships with its own look in inline style attributes and uses React's className. HouseKeeper removes all inline styles and rewrites className to class, so the stylesheet of this site takes over, without changing the third-party code.

Vendor stylesSite styles

Hamburg
19°
Partly cloudy · feels like 17°
⛅
  • Mon
    ☀️
    24° 14°
  • Tue
    ⛅
    21° 13°
  • Wed
    🌧️
    17° 11°
  • Thu
    ⛈️
    16° 10°
  • Fri
    🌤️
    22° 12°