Skip to content

Commit 9ba9e62

Browse files
committed
Add example of importing a react component from JS
There is currently no documentation showing how to import an external react component into ReScript. This commit adds one, adopting an example from https://forum.rescript-lang.org/t/use-reactjs-components-in-rescript-react-project/1992
1 parent 3496595 commit 9ba9e62

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

data/sidebar_react_latest.json

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
{
2-
"Overview": [
3-
"introduction",
4-
"installation",
5-
"migrate-from-reason-react"
6-
],
2+
"Overview": ["introduction", "installation", "migrate-from-reason-react"],
73
"Main Concepts": [
84
"elements-and-jsx",
95
"rendering-elements",
@@ -12,7 +8,8 @@
128
"refs-and-the-dom",
139
"context",
1410
"styling",
15-
"router"
11+
"router",
12+
"javascript-interop"
1613
],
1714
"Hooks & State Management": [
1815
"hooks-overview",
@@ -23,8 +20,5 @@
2320
"hooks-ref",
2421
"hooks-custom"
2522
],
26-
"Guides": [
27-
"beyond-jsx",
28-
"forwarding-refs"
29-
]
23+
"Guides": ["beyond-jsx", "forwarding-refs"]
3024
}

pages/docs/manual/latest/import-from-export-to-js.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ canonical: "/docs/manual/latest/import-from-export-to-js"
88

99
You've seen how ReScript's idiomatic [Import & Export](import-export.md) works. This section describes how we work with importing stuff from JavaScript and exporting stuff for JavaScript consumption.
1010

11+
If you're looking for react-specific interop guidance, check out the [React JS Interop guide](../../react/latest/javascript-interop.mdx).
12+
1113
**Note**: due to JS ecosystem's module compatibility issues, our advice of keeping your ReScript file's compiled JS output open in a tab applies here **more than ever**, as you don't want to subtly output the wrong JS module import/export code, on top of having to deal with Babel/Webpack/Jest/Node's CommonJS<->ES6 compatibility shims.
1214

1315
In short: **make sure your bindings below output what you'd have manually written in JS**.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: "JavaScript Interop"
3+
description: "Working with React module content in ReScript"
4+
canonical: "/docs/react/latest/javascript-interop"
5+
---
6+
7+
# JavaScript Interop
8+
9+
This section complements the general [Import from / Export to JS](../../manual/latest/import-from-export-to-js.md)
10+
section with React-specific guidance.
11+
12+
## Import from JavaScript
13+
14+
### Importing a JS-based React Component
15+
16+
This is especially useful for 3rd-party components.
17+
18+
<CodeTab labels={["ReScript", "JS Output (ES6)"]}>
19+
20+
```res example
21+
module DatePicker = {
22+
@module("react-datepicker") @react.component
23+
external make: (~date: Js.Date.t, ~onChange: (Js.Date.t) => unit) => React.element = "default"
24+
}
25+
26+
@react.component
27+
let make = () => {
28+
let (date, setDate) = React.useState(Js.Date.make)
29+
<DatePicker date=date onChange={date => setDate(_ => date)} />
30+
}
31+
```
32+
```js
33+
import * as Curry from "./stdlib/curry.js";
34+
import * as React from "react";
35+
import ReactDatepicker from "react-datepicker";
36+
37+
var DatePicker = {};
38+
39+
function Playground(Props) {
40+
var match = React.useState(function () {
41+
return new Date();
42+
});
43+
var setDate = match[1];
44+
return React.createElement(ReactDatepicker, {
45+
date: match[0],
46+
onChange: (function (date) {
47+
return Curry._1(setDate, (function (param) {
48+
return date;
49+
}));
50+
})
51+
});
52+
}
53+
```
54+
55+
</CodeTab>

0 commit comments

Comments
 (0)