reasonml / reason-react

Reason bindings for ReactJS
https://reasonml.github.io/reason-react/
MIT License
3.25k stars 347 forks source link

Support "custom children" in uppercase components #822

Open jchavarri opened 9 months ago

jchavarri commented 9 months ago

Consider the following component:

module Test = {
  type t = {foo: int};
  [@react.component]
  let make = (~children) => {
    let () = Array.iter(c => Js.log(c.foo), children);
    <div />;
  };
};

An a usage:

let el = <Test> {foo: 2} {foo:3} </Test>

This fails with

11 |   ((Test.createElement ~children:[{ Test.foo = 2 }; { Test.foo = 3 }] ())
                                       ^^^^^^^^^^^^^^^^
Error: This expression has type Test.t but an expression was expected of type React.element

The reason is that the PPX compiles the above into:

let el =
  React.jsxs(
    Test.make,
    Test.makeProps(
      ~children=React.array([|{Test.foo: 2}, {Test.foo: 3}|]),
      (),
    ),
  );

And React.array expects (correctly) an array of elements as input.

I wonder if the PPX could skip the call to React.array for elements that are produced using custom components (i.e. the upper case ones)? In those cases, the component author has full control, so if they pass an array, we can allow the type inside the array to be anything, really.

davesnx commented 3 months ago

This will force all usages of children to be either fragments, or wrapped in React.array.