tyxml icon indicating copy to clipboard operation
tyxml copied to clipboard

Jsx and optional arguments

Open jfrolich opened this issue 5 years ago • 3 comments

If I do the following

<h1 ?id>...contents</h1>

I get the warning of "unexpected optional jsx attribute" while id is an optional argument on h1. Would be great to have this supported!

jfrolich avatar Jun 20 '20 15:06 jfrolich

Hmm, that's interesting. I didn't remember this syntax being accepted by JSX. id isn't really an "optional argument" in the usual meaning of the word. We could expand the code to make it work though, that would be a nice feature.

Drup avatar Jun 22 '20 13:06 Drup

Yes, id here is just a (bad) example. Allowing optional arguments makes it quite powerful!

jfrolich avatar Jun 22 '20 14:06 jfrolich

Faced a similar problem today. I was expecting this kind of code to work

module Demo = {
  let createElement = (~opt=?, ()) => {
    switch (opt) {
    | None => ()
    | Some(_) => ()
    };
  };
};

let opt = None
let () = <Demo />
let () = <Demo ?opt />

Apparently it works fine with the reactjs jsx https://reasonml.github.io/en/try?rrjsx=true&reason=NoAQRgzgdAxg9gOwGYEsDmACA3gKwgDwC4MBmAXwF0BuAKBoFs4ATAVwBsBTDAcQCcOOAFxQJMAXmw0MGUPwCGMQbDj0ADog4JBFKRk6CM9OQGsuEgBQA-JHDhiARACZ7ASgxiAfJOnSAPGBZBQUQvLAAlDgUlCEFeETRzGzgXMgxfAHoAoJDdMlo8un0MJPcMADkNWgiogBEAeQBZCKh+BCYOXgAVOABRTnpNQQB1FEEACwBJJnNfPgFhUQwAfiT0jwAaDHtVfgA3FA4Ad1cqIA

Khady avatar Feb 08 '21 08:02 Khady