Skip to content

Commit

Permalink
@react.componentWithProps with use memo (#7300)
Browse files Browse the repository at this point in the history
* Exploring problem space

* Copy attributes from function

* Remove moved attributes from expr

* Update test

* Add changelog entry
  • Loading branch information
nojaf authored Feb 21, 2025
1 parent 2f38459 commit 26a15aa
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- Fix runtime type check for Object in untagged variants when one variant case is `null`. https://github.com/rescript-lang/rescript/pull/7303
- Fix files that were being truncated when sent to the CDN over FTP. https://github.com/rescript-lang/rescript/pull/7306
- Fix better editor completion for applications. https://github.com/rescript-lang/rescript/pull/7291
- Fix @react.componentWithProps no longer works with @directive("'use memo'"). https://github.com/rescript-lang/rescript/pull/7300

#### :house: Internal

Expand Down
7 changes: 7 additions & 0 deletions compiler/syntax/src/jsx_v4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,7 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =

let wrapper_expr =
Exp.fun_ ~arity:None Nolabel None props_pattern
~attrs:binding.pvb_expr.pexp_attributes
(Jsx_common.async_component ~async:is_async
(Exp.apply
(Exp.ident
Expand Down Expand Up @@ -1282,6 +1283,12 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
{
binding with
pvb_attributes = binding.pvb_attributes |> List.filter other_attrs_pure;
pvb_expr =
{
binding.pvb_expr with
(* moved to wrapper_expr *)
pexp_attributes = [];
};
},
new_binding )
else (None, binding, None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,15 @@ module V4A6 = {
\"SharedPropsWithProps$V4A6"
}
}

module V4A7 = {
type props = {count: int}

let make = props => {
React.int(props.count)
}
let make = {
let \"SharedPropsWithProps$V4A7" = @directive("'use memo'") props => make(props)
\"SharedPropsWithProps$V4A7"
}
}
10 changes: 10 additions & 0 deletions tests/syntax_tests/data/ppx/react/sharedPropsWithProps.res
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ module V4A6 = {
}
}
}

module V4A7 = {
type props = {count: int}

@react.componentWithProps
let make = @directive("'use memo'")
props => {
React.int(props.count)
}
}
10 changes: 10 additions & 0 deletions tests/tests/src/alias_default_value_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ let C7 = {
make: Alias_default_value_test$C7
};

function Alias_default_value_test$C8(props) {
'use memo';
return props.count;
}

let C8 = {
make: Alias_default_value_test$C8
};

export {
C0,
C1,
Expand All @@ -90,5 +99,6 @@ export {
C4,
C6,
C7,
C8,
}
/* No side effect */
11 changes: 11 additions & 0 deletions tests/tests/src/alias_default_value_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,14 @@ module C7 = {
React.string(`Hello ${name}, you clicked me ` ++ times)
}
}

module C8 = {
type props = {count: int}

@react.componentWithProps
let make =
@directive("'use memo'")
props => {
React.int(props.count)
}
}

1 comment on commit 26a15aa

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Syntax Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: 26a15aa Previous: 80744a5 Ratio
Print Napkinscript.res - time/run 108.51613004666666 ms 102.03488166 ms 1.06
Parse HeroGraphic.res - time/run 8.100807186666666 ms 7.62831228 ms 1.06
Print HeroGraphic.res - time/run 13.694398799999998 ms 12.308145193333333 ms 1.11

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.