Skip to content
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

fix: replace React.createElement with jsx runtime #8043

Draft
wants to merge 1 commit into
base: next
Choose a base branch
from

Conversation

stipsan
Copy link
Member

@stipsan stipsan commented Dec 13, 2024

Description

Before the JSX transform were introduced it didn't really matter if you called React.createElement directly, or if you used JSX.
With

export default function Component(props) {
  const {icon: Icon} = props

  return <Icon />
}

as input, you got

export default function Component(props) {
  var Icon = props.icon;
  return /*#__PURE__*/ React.createElement(Icon, null);
}

as output. In that world you might as well call it directly, as it's functionally equivalent:

import {createElement} from 'react'

export default function Component(props) {
  return createElement(props.icon)
}

Since then the entire ecosystem has moved to the jsx transform, that includes our vite when using sanity dev|build|deploy, as well as publishing libraries with @sanity/pkg-utils, and most other build tooling like Parcel, tsup etc etc.

With the new transform it's not just a new function being used, it's from a different, standalone export, and its function argument semantics are different:

import { jsx as _jsx } from "react/jsx-runtime";
export default function Component(props) {
  var Icon = props.icon;
  return /*#__PURE__*/ _jsx(Icon, {});
}

On top of that, React 19 introduces new features that are only available if the jsx-runtime transform is used, which isn't supported with createElement.
For example using ref as a prop, instead of using React.forwardRef, requires it:

// React 18 requires a wrapper in function components to forward refs to the dom node
const LoadingIcon = React.forwardRef(
  function(props, ref) {
    return <svg ref={ref} />
  }
)

// React 19 supports ref as a prop
function LoadingIcon(props) {
  return <svg ref={props.ref} />
}

The React 19 <LoadingIcon /> only works if the JSX transform is used (repl):

export default function Component(props) {
  const {icon: Icon} = props
  const ref = useRef()

  return <Icon ref={ref} />
}

// Called as <Component icon={LoadingIcon} />

When using createElement it fails (repl):

import {createElement, useRef} from 'react'

export default function Component(props) {
  const ref = useRef()

  // Due to `createElement` being used, the `ref` won't be set since `LoadingIcon` isn't wrapped in `React.forwardRef`
  return createElement(props.icon, {ref})
}

What to review

Is the linter rule addition that blocks React.createElement clear enough?

Testing

If tests pass we should be good.

Notes for release

Replaced React.createElement calls in internals with the JSX runtime, unlocking the React 19 performance improvements to JSX, as well as the ability to use ref as a prop instead of React.forwardRef wrappers.

Copy link

vercel bot commented Dec 13, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
page-building-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 13, 2024 5:33pm
performance-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 13, 2024 5:33pm
test-next-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 13, 2024 5:33pm
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 13, 2024 5:33pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Dec 13, 2024 5:33pm

Copy link
Contributor

No changes to documentation

Copy link
Contributor

Component Testing Report Updated Dec 13, 2024 5:41 PM (UTC)

❌ Failed Tests (6) -- expand for details
File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 1m 13s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 14s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ✅ Passed (Inspect) 41s 6 0 0
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 57s 11 7 0
formBuilder/inputs/PortableText/copyPaste/CopyPasteFields.spec.tsx ✅ Passed (Inspect) 0s 0 12 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 29s 6 0 0
formBuilder/inputs/PortableText/DisableFocusAndUnset.spec.tsx ✅ Passed (Inspect) 16s 3 0 0
formBuilder/inputs/PortableText/DragAndDrop.spec.tsx ❌ Failed (Inspect) 4m 27s 0 0 6
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ✅ Passed (Inspect) 1m 14s 15 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 3m 0s 21 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 1m 49s 18 0 0
formBuilder/inputs/PortableText/PresenceCursors.spec.tsx ✅ Passed (Inspect) 14s 3 9 0
formBuilder/inputs/PortableText/RangeDecoration.spec.tsx ✅ Passed (Inspect) 42s 9 0 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 29s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ✅ Passed (Inspect) 1m 53s 21 0 0
formBuilder/tree-editing/TreeEditing.spec.tsx ✅ Passed (Inspect) 0s 0 3 0
formBuilder/tree-editing/TreeEditingNestedObjects.spec.tsx ✅ Passed (Inspect) 0s 0 3 0

Copy link
Contributor

⚡️ Editor Performance Report

Updated Fri, 13 Dec 2024 17:45:50 GMT

Benchmark reference
latency of sanity@latest
experiment
latency of this branch
Δ (%)
latency difference
article (title) 26.3 efps (38ms) 25.6 efps (39ms) +1ms (+2.6%)
article (body) 65.4 efps (15ms) 72.7 efps (14ms) -2ms (-/-%)
article (string inside object) 27.0 efps (37ms) 26.3 efps (38ms) +1ms (+2.7%)
article (string inside array) 25.0 efps (40ms) 25.0 efps (40ms) +0ms (-/-%)
recipe (name) 52.6 efps (19ms) 50.0 efps (20ms) +1ms (+5.3%)
recipe (description) 60.6 efps (17ms) 58.8 efps (17ms) +1ms (+3.0%)
recipe (instructions) 99.9+ efps (5ms) 99.9+ efps (5ms) +0ms (-/-%)
synthetic (title) 18.9 efps (53ms) 18.9 efps (53ms) +0ms (-/-%)
synthetic (string inside object) 18.9 efps (53ms) 18.9 efps (53ms) +0ms (-/-%)

efps — editor "frames per second". The number of updates assumed to be possible within a second.

Derived from input latency. efps = 1000 / input_latency

Detailed information

🏠 Reference result

The performance result of sanity@latest

Benchmark latency p75 p90 p99 blocking time test duration
article (title) 38ms 41ms 43ms 159ms 177ms 10.3s
article (body) 15ms 17ms 23ms 83ms 286ms 5.5s
article (string inside object) 37ms 39ms 46ms 73ms 127ms 6.4s
article (string inside array) 40ms 41ms 44ms 154ms 132ms 6.8s
recipe (name) 19ms 20ms 25ms 42ms 0ms 7.1s
recipe (description) 17ms 18ms 21ms 39ms 0ms 4.4s
recipe (instructions) 5ms 7ms 9ms 30ms 0ms 3.2s
synthetic (title) 53ms 54ms 57ms 86ms 208ms 12.4s
synthetic (string inside object) 53ms 56ms 67ms 200ms 550ms 8.1s

🧪 Experiment result

The performance result of this branch

Benchmark latency p75 p90 p99 blocking time test duration
article (title) 39ms 42ms 46ms 233ms 245ms 11.6s
article (body) 14ms 17ms 24ms 48ms 64ms 4.7s
article (string inside object) 38ms 40ms 44ms 192ms 273ms 6.7s
article (string inside array) 40ms 42ms 47ms 163ms 148ms 6.7s
recipe (name) 20ms 22ms 24ms 32ms 0ms 7.5s
recipe (description) 17ms 19ms 20ms 35ms 0ms 4.4s
recipe (instructions) 5ms 7ms 8ms 9ms 0ms 3.0s
synthetic (title) 53ms 58ms 69ms 127ms 588ms 13.6s
synthetic (string inside object) 53ms 54ms 62ms 120ms 213ms 7.5s

📚 Glossary

column definitions

  • benchmark — the name of the test, e.g. "article", followed by the label of the field being measured, e.g. "(title)".
  • latency — the time between when a key was pressed and when it was rendered. derived from a set of samples. the median (p50) is shown to show the most common latency.
  • p75 — the 75th percentile of the input latency in the test run. 75% of the sampled inputs in this benchmark were processed faster than this value. this provides insight into the upper range of typical performance.
  • p90 — the 90th percentile of the input latency in the test run. 90% of the sampled inputs were faster than this. this metric helps identify slower interactions that occurred less frequently during the benchmark.
  • p99 — the 99th percentile of the input latency in the test run. only 1% of sampled inputs were slower than this. this represents the worst-case scenarios encountered during the benchmark, useful for identifying potential performance outliers.
  • blocking time — the total time during which the main thread was blocked, preventing user input and UI updates. this metric helps identify performance bottlenecks that may cause the interface to feel unresponsive.
  • test duration — how long the test run took to complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant