There are three approaches to interop with JavaScript code in Js_of_ocaml:
- Use the functions available in the
Js
module in order to translate the data inside Reason/OCaml files, so it can be used from JavaScript. - Use OCaml
external
statements to define the types, and then do the transformations on the JavaScript side. External functions are the same way that OCaml offers to interop with C code as well. - Use
gen_js_api
and create interface files so that the tool generates all the mapping code for you :)
The approach being used in jsoo-react
is the third, as it automates all the conversions needed and doesn't require to think about them
Some notes on how interop in BuckleScript and gen_js_api relate:
Feature | BuckleScript interop | gen_js_api interface files |
---|---|---|
Use JS data types | array , string |
No work needed, conversions will be added in generated code automatically |
Require JS modules | [@bs.module] |
Add require("my-lib") statement in .js file (example), include it in Dune (example) |
Bind to JS values | [@bs.val] |
[@js.global] |
Variadic params | [@bs.splice] |
[@js.variadic] |
Raw expressions | [%bs.raw] |
Js.Unsafe.js_expr(); |
Create JS objects | [%bs.obj] |
[@js.builder] |
Access JS objects properties | ## |
##. |
Nullable values | Js.nullable |
No work needed, conversions will be added in generated code automatically |
For more information about gen_js_api
, check the project documentation.