Skip to content

Commit 26a15aa

Browse files
authoredFeb 21, 2025··
@react.componentWithProps with use memo (#7300)
* Exploring problem space * Copy attributes from function * Remove moved attributes from expr * Update test * Add changelog entry
1 parent 2f38459 commit 26a15aa

File tree

6 files changed

+51
-0
lines changed

6 files changed

+51
-0
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
- Fix runtime type check for Object in untagged variants when one variant case is `null`. https://github.com/rescript-lang/rescript/pull/7303
3737
- Fix files that were being truncated when sent to the CDN over FTP. https://github.com/rescript-lang/rescript/pull/7306
3838
- Fix better editor completion for applications. https://github.com/rescript-lang/rescript/pull/7291
39+
- Fix @react.componentWithProps no longer works with @directive("'use memo'"). https://github.com/rescript-lang/rescript/pull/7300
3940

4041
#### :house: Internal
4142

‎compiler/syntax/src/jsx_v4.ml

+7
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,7 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
12441244

12451245
let wrapper_expr =
12461246
Exp.fun_ ~arity:None Nolabel None props_pattern
1247+
~attrs:binding.pvb_expr.pexp_attributes
12471248
(Jsx_common.async_component ~async:is_async
12481249
(Exp.apply
12491250
(Exp.ident
@@ -1282,6 +1283,12 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
12821283
{
12831284
binding with
12841285
pvb_attributes = binding.pvb_attributes |> List.filter other_attrs_pure;
1286+
pvb_expr =
1287+
{
1288+
binding.pvb_expr with
1289+
(* moved to wrapper_expr *)
1290+
pexp_attributes = [];
1291+
};
12851292
},
12861293
new_binding )
12871294
else (None, binding, None)

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

+12
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,15 @@ module V4A6 = {
131131
\"SharedPropsWithProps$V4A6"
132132
}
133133
}
134+
135+
module V4A7 = {
136+
type props = {count: int}
137+
138+
let make = props => {
139+
React.int(props.count)
140+
}
141+
let make = {
142+
let \"SharedPropsWithProps$V4A7" = @directive("'use memo'") props => make(props)
143+
\"SharedPropsWithProps$V4A7"
144+
}
145+
}

‎tests/syntax_tests/data/ppx/react/sharedPropsWithProps.res

+10
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,13 @@ module V4A6 = {
9191
}
9292
}
9393
}
94+
95+
module V4A7 = {
96+
type props = {count: int}
97+
98+
@react.componentWithProps
99+
let make = @directive("'use memo'")
100+
props => {
101+
React.int(props.count)
102+
}
103+
}

‎tests/tests/src/alias_default_value_test.mjs

+10
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ let C7 = {
8282
make: Alias_default_value_test$C7
8383
};
8484

85+
function Alias_default_value_test$C8(props) {
86+
'use memo';
87+
return props.count;
88+
}
89+
90+
let C8 = {
91+
make: Alias_default_value_test$C8
92+
};
93+
8594
export {
8695
C0,
8796
C1,
@@ -90,5 +99,6 @@ export {
9099
C4,
91100
C6,
92101
C7,
102+
C8,
93103
}
94104
/* No side effect */

‎tests/tests/src/alias_default_value_test.res

+11
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,14 @@ module C7 = {
6464
React.string(`Hello ${name}, you clicked me ` ++ times)
6565
}
6666
}
67+
68+
module C8 = {
69+
type props = {count: int}
70+
71+
@react.componentWithProps
72+
let make =
73+
@directive("'use memo'")
74+
props => {
75+
React.int(props.count)
76+
}
77+
}

0 commit comments

Comments
 (0)