From ba953795fcb4f7e2e1ffc47da77e0472191d767e Mon Sep 17 00:00:00 2001 From: John Haley Date: Wed, 30 Apr 2025 16:02:14 +0100 Subject: [PATCH 1/2] add reason-react binding examples to cookbook --- docs/bindings-cookbook.md | 154 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/docs/bindings-cookbook.md b/docs/bindings-cookbook.md index 8a4f4d39d..3dc80cee2 100644 --- a/docs/bindings-cookbook.md +++ b/docs/bindings-cookbook.md @@ -662,3 +662,157 @@ Note that this attribute requires the return type of the binding to be an See the [Wrapping returned nullable values](./working-with-js-objects-and-values.md#wrapping-returned-nullable-values) section for more information. + +## React Components + +The following examples show how to bind to and use React components that were written in JavaScript. These bindings allow seamless integration between OCaml/ReasonML and JavaScript React components. + +### Basic component binding + +To bind to a simple React component from JavaScript: + +```ocaml +module Button = struct + external make : label:string -> onClick:(unit -> unit) -> React.element + = "default" + [@@mel.module "./ButtonInJavaScript.js"] [@@react.component] +end + +(* Usage *) +Button.createElement ~label:"Click me" + ~onClick:(fun () -> Js.log "Clicked!") + ~children:[] () [@JSX] + +``` +```reasonml +module Button = { + [@mel.module "./ButtonInJavaScript.js"] [@react.component] + external make: (~label: string, ~onClick: unit => unit) => React.element = + "default"; +}; + +// Usage +