Reason-react ffi

I’ve been playing with Rescript for a few days and am absolutely loving it. I think it’s going to dethrone typescript as Javascript’s successor, and that’s super exciting. Once I ramp up, I’m hoping I can contribute to the project (probably docs).

For now, though, I have a n00b question. I’m building a toy ReasonReact app and am having trouble accessing an event target. The ReasonReact docs suggest this Reason syntax:

/* App.re */
/* CORRECT! This code is bug-free. 👍 */
[@react.component]
let make = () => {
  let (name, setName) = React.useState(() => "John");
  <input
    type_="text"
    value={name}
    onChange={
      event => {
        let value = ReactEvent.Form.target(event)##value;
        setName(_ => value)
      }
    }
  />;
};

Rescript doesn’t like this, though; the compiler is unhappy with the ## syntax. I’m guessing this was removed in Rescript because it doesn’t look like JS, but what was it replaced with?

The a##b syntax is the Reason object access, which was changed in ReScript. The ReScript version is a["b"] (docs)

Until the ReasonReact docs get updated to use ReScript syntax, you can convert between the two syntaxes in the online playground. (Convert in the “settings” tab.)

The ReScript version of that code is this:

/* App.re */
/* CORRECT! This code is bug-free. 👍 */
@react.component
let make = () => {
  let (name, setName) = React.useState(() => "John")
  <input
    type_="text"
    value=name
    onChange={event => {
      let value = ReactEvent.Form.target(event)["value"]
      setName(_ => value)
    }}
  />
}

Awesome! I knew it would be something simple. Thanks for the tip on converting syntaxes, I’ll be using that a lot.

Just so you know, the new rescript-react refactor is right around the corner, which means the new docs will also be curated in ReScript syntax. Hopefully this will make things easier for you later on.

Don’t worry, the APIs won’t change (only the names).

3 Likes

Yeah, I can see everything is in a state of flux right now, but that makes it that much more exciting. :slight_smile: It’s fun to come on on the ground floor of something that I think is going to be huge.

3 Likes