Skip to content

Commit c32e6fd

Browse files
committed
build error using @react.componentWithProps with React.forwardRef
1 parent 7c0e030 commit c32e6fd

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

compiler/syntax/src/jsx_v4.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,22 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
12161216
let make_new_binding ~loc ~full_module_name binding =
12171217
let props_pattern =
12181218
match binding.pvb_expr with
1219+
| {pexp_desc = Pexp_apply (wrapper_expr, [(Nolabel, func_expr)])}
1220+
when is_forward_ref wrapper_expr ->
1221+
(* Case when using React.forwardRef *)
1222+
let rec check_invalid_forward_ref expr =
1223+
match expr.pexp_desc with
1224+
| Pexp_fun ((Labelled _ | Optional _), _, _, _, _) ->
1225+
Location.raise_errorf ~loc:expr.pexp_loc
1226+
"Components using React.forwardRef cannot use \
1227+
@react.componentWithProps. Please use @react.component \
1228+
instead."
1229+
| Pexp_fun (Nolabel, _, _, body, _) ->
1230+
check_invalid_forward_ref body
1231+
| _ -> ()
1232+
in
1233+
check_invalid_forward_ref func_expr;
1234+
Pat.var {txt = "props"; loc}
12191235
| {
12201236
pexp_desc =
12211237
Pexp_fun (_, _, {ppat_desc = Ppat_constraint (_, typ)}, _, _);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/react_component_with_props.res:4:5-13:10
4+
5+
2 │ @react.componentWithProps
6+
3 │ let make = React.forwardRef((
7+
4 │ ~className=?,
8+
5 │  ~children,
9+
. │ ...
10+
12 │  children
11+
13 │  </div>
12+
14 │ )
13+
15 │ }
14+
15+
Components using React.forwardRef cannot use @react.componentWithProps. Please use @react.component instead.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module V4C7 = {
2+
@react.componentWithProps
3+
let make = React.forwardRef((
4+
~className=?,
5+
~children,
6+
ref: Js.Nullable.t<ReactRef.currentDomRef>,
7+
) =>
8+
<div>
9+
<input
10+
type_="text" ?className ref=?{Js.Nullable.toOption(ref)->Belt.Option.map(React.Ref.domRef)}
11+
/>
12+
children
13+
</div>
14+
)
15+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,4 @@ module V4A6 = {
9090
| #off => React.string("off")
9191
}
9292
}
93-
}
93+
}

0 commit comments

Comments
 (0)