Skip to content

Commit a71276b

Browse files
author
Jack Pope
committed
[compiler] Fix dropped ref with spread props in InlineJsxTransform
1 parent 372ec00 commit a71276b

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

compiler/packages/babel-plugin-react-compiler/src/Optimization/InlineJsxTransform.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -546,16 +546,14 @@ function createPropsProperties(
546546
let refProperty: ObjectProperty | undefined;
547547
let keyProperty: ObjectProperty | undefined;
548548
const props: Array<ObjectProperty | SpreadPattern> = [];
549-
const jsxAttributesWithoutKeyAndRef = propAttributes.filter(
550-
p => p.kind === 'JsxAttribute' && p.name !== 'key' && p.name !== 'ref',
549+
const jsxAttributesWithoutKey = propAttributes.filter(
550+
p => p.kind === 'JsxAttribute' && p.name !== 'key',
551551
);
552552
const jsxSpreadAttributes = propAttributes.filter(
553553
p => p.kind === 'JsxSpreadAttribute',
554554
);
555555
const spreadPropsOnly =
556-
jsxAttributesWithoutKeyAndRef.length === 0 &&
557-
jsxSpreadAttributes.length === 1;
558-
556+
jsxAttributesWithoutKey.length === 0 && jsxSpreadAttributes.length === 1;
559557
propAttributes.forEach(prop => {
560558
switch (prop.kind) {
561559
case 'JsxAttribute': {

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/inline-jsx-transform.expect.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ function ConditionalJsx({shouldWrap}) {
6060
return content;
6161
}
6262

63+
function ComponentWithSpreadPropsAndRef({ref, ...other}) {
64+
return <Foo ref={ref} {...other} />;
65+
}
66+
6367
// TODO: Support value blocks
6468
function TernaryJsx({cond}) {
6569
return cond ? <div /> : null;
@@ -409,6 +413,41 @@ function ConditionalJsx(t0) {
409413
return content;
410414
}
411415

416+
function ComponentWithSpreadPropsAndRef(t0) {
417+
const $ = _c2(6);
418+
let other;
419+
let ref;
420+
if ($[0] !== t0) {
421+
({ ref, ...other } = t0);
422+
$[0] = t0;
423+
$[1] = other;
424+
$[2] = ref;
425+
} else {
426+
other = $[1];
427+
ref = $[2];
428+
}
429+
let t1;
430+
if ($[3] !== other || $[4] !== ref) {
431+
if (DEV) {
432+
t1 = <Foo ref={ref} {...other} />;
433+
} else {
434+
t1 = {
435+
$$typeof: Symbol.for("react.transitional.element"),
436+
type: Foo,
437+
ref: ref,
438+
key: null,
439+
props: { ref: ref, ...other },
440+
};
441+
}
442+
$[3] = other;
443+
$[4] = ref;
444+
$[5] = t1;
445+
} else {
446+
t1 = $[5];
447+
}
448+
return t1;
449+
}
450+
412451
// TODO: Support value blocks
413452
function TernaryJsx(t0) {
414453
const $ = _c2(2);

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/inline-jsx-transform.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ function ConditionalJsx({shouldWrap}) {
5656
return content;
5757
}
5858

59+
function ComponentWithSpreadPropsAndRef({ref, ...other}) {
60+
return <Foo ref={ref} {...other} />;
61+
}
62+
5963
// TODO: Support value blocks
6064
function TernaryJsx({cond}) {
6165
return cond ? <div /> : null;

0 commit comments

Comments
 (0)