Adding Raw JavaScript
The easiest and least type-safe way to access JavaScript is by placing raw JavaScript code in the middle of your ReasonML code. The generic form is: [%raw {| /* Js expression */ |}]. The JavaScript must be an expression, not a series of statements, nor even an expression followed by a semicolon.
What if you really need multiple statements? You can get around the limitation by using an immediately invoked function expression (IIFE). You create an IIFE by putting the statements into an anonymous JavaScript function and then calling the function with a set of parentheses () at the end.
As an example, here’s a program using an IIFE with raw JavaScript to get the current date and time as a string:
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access