When you create a code like this:
[@bs.obj] external makeProps: (~foo: string, ~bar: int=?, unit) => _ = "";
let make = makeProps(~foo="Test", ~bar=1, ());
You’ll get this output:
var make = {
foo: "Test",
bar: 1
};
When you write this:
[@bs.deriving abstract]
type props = {
foo: string,
[@bs.optional] bar: int
};
// bs.deriving abstract gives you a creation function
let make = props(~foo="Test", ~bar=1, ());
You’ll get:
var make = {
foo: "Test",
bar: 1
};
Basically the same. So, when to use one and other? This is my humble opinion
Use [@bs.obj] external makeProps...
when you need to bind something to JS, transform props, etc.
This other approach [@bs.deriving abstract]...
you can use to map something from JS to Reason in a Record Mode BS docs here.
Reproducing BS docs:
If your JS object:
- has a know, fixed set of fields
- might or might not contain values of different types
Record Mode gives you the ability do iterate with object like this:
[%%raw {|var make = { foo: "Test", bar: 1}|}]; // Some external code on JS.
[@bs.deriving abstract]
type props = {
foo: string,
[@bs.optional] bar: int
};
[@bs.val] external make: props = "make";
let foo = make->fooGet; // Do something with foo.
Hope this help you!