Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local abstract (and polymorphic) types and [@react.component] #466

Open
gaku-sei opened this issue Aug 16, 2019 · 4 comments
Open

Local abstract (and polymorphic) types and [@react.component] #466

gaku-sei opened this issue Aug 16, 2019 · 4 comments
Labels
enhancement ppx issues related to the reason-react ppx

Comments

@gaku-sei
Copy link

There seems to be no way to use local abstract types when using [@react.component]:

Defining a polymorphic local abstract type like this:

[@react.component]
let make: type a. (~value: Value.t(a)) => React.element =
  (~value: Value.t(_)) => {
    <input
      value={
        switch (value) {
        | Input.String(value) => value
        | Input.Int(value) => string_of_int(value)
        | _ => ""
        }
      }
    />;
};

Raises a Fatal error: exception Invalid_argument("react.component calls cannot be destructured.").

And defining a simple local abstract type:

[@react.component]
let make = (type a, ~value: Value.t(_)) => {
  <input
    value={
      switch (value) {
      | Value.String(value) => value
      | Value.Int(value) => string_of_int(value)
      | _ => ""
      }
    }
  />;
};

Raises a Fatal error: exception Invalid_argument("react.component calls can only be on function definitions or component wrappers (forwardRef, memo).").

Is there any way to achieve this?

@mlms13
Copy link

mlms13 commented Sep 18, 2019

@jchavarri pointed me in the direction of writing the make and makeProps functions by hand instead of using [@react.component], which worked for what I needed. It would definitely be nice for the PPX to allow annotations, though.

Here's an example of what I ended up doing, in case it helps anyone else: https://github.com/mlms13/csv-reader/blob/8bc543284769bb3308aef80ac2434c575537aca4/src/components/FileInput.re#L46-L57

@baransu
Copy link

baransu commented Sep 22, 2019

I’m just wondering whats a difference between type a. props(a) and props(‘a). I’ve never seen the first version of syntax. Is there any piece of documentation about it?

@anmonteiro
Copy link
Member

@baransu
Copy link

baransu commented Sep 22, 2019

Thanks!

@davesnx davesnx added the ppx issues related to the reason-react ppx label Jul 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement ppx issues related to the reason-react ppx
Projects
None yet
Development

No branches or pull requests

6 participants