Binding to a React component with default value?

Think I’ve seen some bindings before with default values. But I can’t seem to find any examples now :sweat_smile:

What I’m looking for is a way to set a default value, “diffWords”, for method here:

@module("react-string-diff") @react.component
external make: (~oldValue: string, ~newValue: string, ~method: string) => React.element =
  "StringDiff"

You can specify a prop as optional using =? next to the optional property.

@module("react-string-diff") @react.component
external make: (~oldValue: string, ~newValue: string, ~method: string=?) => React.element =
  "StringDiff"

If you can to add a default from ReScript, you’ll need to make a small wrapper around your component.

1 Like

Thanks, that makes sense. And maybe my mind is playing tricks on me. But I think there’s another way to do this directly in the binding :thinking:

It is possible to fix the value in your binding using @as, but then you can’t override it anymore:

@module("react-string-diff") @react.component
external make: (
  ~oldValue: string,
  ~newValue: string,
  ~method: @as("diffWords") _,
) => React.element = "StringDiff"

Playground example

5 Likes

Thank you, this was exactly the example I was looking for :+1: