Skip to content

Commit 0e5d216

Browse files
authored
Add asyncComponent to jsx (#7704)
* Add asyncComponent to jsx * Add changelog entry * Rename to promise
1 parent cc27a5d commit 0e5d216

File tree

7 files changed

+78
-12
lines changed

7 files changed

+78
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
- Fix `--create-sourcedirs` generation with for a single project. https://github.com/rescript-lang/rescript/pull/7671
4040
- Fix rewatch not recompiling on changes under windows. https://github.com/rescript-lang/rescript/pull/7690
4141
- Fix locations of regex literals. https://github.com/rescript-lang/rescript/pull/7683
42+
- Fix async React component compilation. https://github.com/rescript-lang/rescript/pull/7704
4243

4344
# 12.0.0-beta.2
4445

compiler/syntax/src/jsx_common.ml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ let async_component ~async expr =
5353
if async then
5454
let open Ast_helper in
5555
Exp.apply
56-
(Exp.ident
57-
{
58-
loc = Location.none;
59-
txt = Ldot (Lident "JsxPPXReactSupport", "asyncComponent");
60-
})
56+
(Exp.ident {loc = Location.none; txt = Ldot (Lident "Jsx", "promise")})
6157
[(Nolabel, expr)]
6258
else expr

runtime/Jsx.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ type element
3030
external float: float => element = "%identity"
3131
external int: int => element = "%identity"
3232
external string: string => element = "%identity"
33-
3433
external array: array<element> => element = "%identity"
34+
external promise: promise<element> => element = "%identity"
3535

3636
type componentLike<'props, 'return> = 'props => 'return
3737
type component<'props> = componentLike<'props, element>

tests/syntax_tests/data/ppx/react/expected/asyncAwait.res.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module C0 = {
99
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.int(a)})})
1010
}
1111
let make = {
12-
let \"AsyncAwait$C0" = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))
12+
let \"AsyncAwait$C0" = (props: props<_>) => Jsx.promise(make(props))
1313

1414
\"AsyncAwait$C0"
1515
}
@@ -26,7 +26,7 @@ module C1 = {
2626
}
2727
}
2828
let make = {
29-
let \"AsyncAwait$C1" = (props: props<_>) => JsxPPXReactSupport.asyncComponent(make(props))
29+
let \"AsyncAwait$C1" = (props: props<_>) => Jsx.promise(make(props))
3030

3131
\"AsyncAwait$C1"
3232
}

tests/syntax_tests/data/ppx/react/expected/sharedPropsWithProps.res.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ module V4A5 = {
4545
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.int(a)})})
4646
}
4747
let make = {
48-
let \"SharedPropsWithProps$V4A5" = (props: props<_>) =>
49-
JsxPPXReactSupport.asyncComponent(make(props))
48+
let \"SharedPropsWithProps$V4A5" = (props: props<_>) => Jsx.promise(make(props))
5049
\"SharedPropsWithProps$V4A5"
5150
}
5251
}
@@ -60,8 +59,7 @@ module V4A6 = {
6059
}
6160
}
6261
let make = {
63-
let \"SharedPropsWithProps$V4A6" = (props: props<_>) =>
64-
JsxPPXReactSupport.asyncComponent(make(props))
62+
let \"SharedPropsWithProps$V4A6" = (props: props<_>) => Jsx.promise(make(props))
6563
\"SharedPropsWithProps$V4A6"
6664
}
6765
}

tests/tests/src/async_jsx.mjs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as JsxRuntime from "react/jsx-runtime";
4+
5+
function getNow() {
6+
return new Promise((res, param) => {
7+
setTimeout(() => res(new Date()), 1000);
8+
});
9+
}
10+
11+
async function make(param) {
12+
let now = await getNow();
13+
return <div>
14+
<p>
15+
{now.toLocaleString()}
16+
</p>
17+
</div>;
18+
}
19+
20+
let Async_jsx$Foo = make;
21+
22+
let Foo = {
23+
make: Async_jsx$Foo
24+
};
25+
26+
function Async_jsx$Bar(props) {
27+
return <div>
28+
<Async_jsx$Foo />
29+
</div>;
30+
}
31+
32+
let Bar = {
33+
make: Async_jsx$Bar
34+
};
35+
36+
export {
37+
getNow,
38+
Foo,
39+
Bar,
40+
}
41+
/* react/jsx-runtime Not a pure module */

tests/tests/src/async_jsx.res

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
@@config({
2+
flags: ["-bs-jsx", "4", "-bs-jsx-preserve"],
3+
})
4+
5+
let getNow = () => {
6+
Promise.make((res, _) => {
7+
setTimeout(() => {
8+
res(Date.make())
9+
}, 1000)->ignore
10+
})
11+
}
12+
13+
module Foo = {
14+
@react.component
15+
let make = async () => {
16+
let now = await getNow()
17+
<div>
18+
<p> {React.string(now->Date.toLocaleString)} </p>
19+
</div>
20+
}
21+
}
22+
23+
module Bar = {
24+
@react.component
25+
let make = () => {
26+
<div>
27+
<Foo />
28+
</div>
29+
}
30+
}

0 commit comments

Comments
 (0)