Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 882a499

Browse files
mununkicristianoc
authored andcommitted
use React.createElementWithKey
instead Jsx.addKeyProp directly
1 parent e68b5bc commit 882a499

File tree

8 files changed

+116
-27
lines changed

8 files changed

+116
-27
lines changed

cli/reactjs_jsx_v4.ml

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -397,23 +397,18 @@ let transformUppercaseCall3 ~config modulePath mapper jsxExprLoc callExprLoc
397397
]
398398
@ key)
399399
| _ -> (
400-
let keyAddedProps ~keyExpr =
401-
let propsType =
402-
Typ.constr (Location.mknoloc @@ ident ~suffix:"props") [Typ.any ()]
403-
in
404-
Exp.apply
405-
(Exp.ident
406-
{loc = Location.none; txt = Ldot (Lident "Jsx", "addKeyProp")})
407-
[(nolabel, Exp.constraint_ props propsType); (nolabel, keyExpr)]
408-
in
409400
match (!childrenArg, keyProp) with
410401
| None, (_, keyExpr) :: _ ->
411402
Exp.apply ~attrs
412403
(Exp.ident
413-
{loc = Location.none; txt = Ldot (Lident "React", "createElement")})
404+
{
405+
loc = Location.none;
406+
txt = Ldot (Lident "React", "createElementWithKey");
407+
})
414408
[
415409
(nolabel, Exp.ident {txt = ident ~suffix:"make"; loc = callExprLoc});
416-
(nolabel, keyAddedProps ~keyExpr);
410+
(nolabel, props);
411+
(nolabel, keyExpr);
417412
]
418413
| None, [] ->
419414
Exp.apply ~attrs
@@ -428,12 +423,13 @@ let transformUppercaseCall3 ~config modulePath mapper jsxExprLoc callExprLoc
428423
(Exp.ident
429424
{
430425
loc = Location.none;
431-
txt = Ldot (Lident "React", "createElementVariadic");
426+
txt = Ldot (Lident "React", "createElementVariadicWithKey");
432427
})
433428
[
434429
(nolabel, Exp.ident {txt = ident ~suffix:"make"; loc = callExprLoc});
435-
(nolabel, keyAddedProps ~keyExpr);
430+
(nolabel, props);
436431
(nolabel, children);
432+
(nolabel, keyExpr);
437433
]
438434
| Some children, [] ->
439435
Exp.apply ~attrs
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module A = {
2+
type props<'x> = {x: 'x}
3+
@react.component let make = ({x, _}: props<'x>) => React.string(x)
4+
let make = {
5+
let \"Interface$A" = (props: props<_>) => make(props)
6+
\"Interface$A"
7+
}
8+
}
9+
10+
module NoProps = {
11+
type props = {}
12+
13+
@react.component let make = (_: props) => ReactDOM.jsx("div", {})
14+
let make = {
15+
let \"Interface$NoProps" = props => make(props)
16+
17+
\"Interface$NoProps"
18+
}
19+
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1-
type props<'x> = {x: 'x}
2-
let make: React.componentLike<props<string>, React.element>
1+
module A: {
2+
type props<'x> = {x: 'x}
3+
let make: React.componentLike<props<string>, React.element>
4+
}
5+
6+
module NoProps: {
7+
type props = {}
8+
9+
let make: React.componentLike<props, React.element>
10+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
@@jsxConfig({version: 4, mode: "classic"})
2+
3+
module V4CA = {
4+
type props = {}
5+
6+
@react.component let make = (_: props) => ReactDOMRe.createDOMElementVariadic("div", [])
7+
let make = {
8+
let \"NoPropsWithKey$V4CA" = props => make(props)
9+
10+
\"NoPropsWithKey$V4CA"
11+
}
12+
}
13+
14+
module V4CB = {
15+
type props = {}
16+
17+
@module("c")
18+
external make: React.componentLike<props, React.element> = "component"
19+
}
20+
21+
module V4C = {
22+
type props = {}
23+
24+
@react.component
25+
let make = (_: props) =>
26+
ReactDOMRe.createElement(
27+
ReasonReact.fragment,
28+
[
29+
React.createElementWithKey(V4CA.make, {}, "k"),
30+
React.createElementWithKey(V4CB.make, {}, "k"),
31+
],
32+
)
33+
let make = {
34+
let \"NoPropsWithKey$V4C" = props => make(props)
35+
36+
\"NoPropsWithKey$V4C"
37+
}
38+
}

tests/ppx/react/expected/removedKeyProp.res.txt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,14 @@ let make = (_: props) =>
3030
ReactDOMRe.createElement(
3131
ReasonReact.fragment,
3232
[
33-
React.createElement(Foo.make, Jsx.addKeyProp(({x: "x", y: "y"}: Foo.props<_>), "k")),
33+
React.createElementWithKey(Foo.make, {x: "x", y: "y"}, "k"),
3434
React.createElement(Foo.make, {x: "x", y: "y"}),
35-
React.createElement(
35+
React.createElementWithKey(
3636
HasChildren.make,
37-
Jsx.addKeyProp(
38-
(
39-
{
40-
children: React.createElement(Foo.make, {x: "x", y: "y"}),
41-
}: HasChildren.props<_>
42-
),
43-
"k",
44-
),
37+
{
38+
children: React.createElement(Foo.make, {x: "x", y: "y"}),
39+
},
40+
"k",
4541
),
4642
React.createElement(
4743
HasChildren.make,

tests/ppx/react/interface.res

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module A = {
2+
@react.component
3+
let make = (~x) => React.string(x)
4+
}
5+
6+
module NoProps = {
7+
@react.component
8+
let make = () => <div />
9+
}

tests/ppx/react/interface.resi

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
@react.component
2-
let make: (~x:string) => React.element
1+
module A: {
2+
@react.component
3+
let make: (~x: string) => React.element
4+
}
5+
6+
module NoProps: {
7+
@react.component
8+
let make: unit => React.element
9+
}

tests/ppx/react/noPropsWithKey.res

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@@jsxConfig({version: 4, mode: "classic"})
2+
3+
module V4CA = {
4+
@react.component
5+
let make = () => <div />
6+
}
7+
8+
module V4CB = {
9+
@module("c") @react.component
10+
external make: unit => React.element = "component"
11+
}
12+
13+
module V4C = {
14+
@react.component
15+
let make = () => <><V4CA key="k" /> <V4CB key="k" /></>
16+
}

0 commit comments

Comments
 (0)