Skip to content

Add example of importing a react component from JS #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions data/sidebar_react_latest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{
"Overview": [
"introduction",
"installation",
"migrate-from-reason-react"
],
"Overview": ["introduction", "installation", "migrate-from-reason-react"],
"Main Concepts": [
"elements-and-jsx",
"rendering-elements",
Expand All @@ -12,7 +8,8 @@
"refs-and-the-dom",
"context",
"styling",
"router"
"router",
"javascript-interop"
],
"Hooks & State Management": [
"hooks-overview",
Expand All @@ -23,8 +20,5 @@
"hooks-ref",
"hooks-custom"
],
"Guides": [
"beyond-jsx",
"forwarding-refs"
]
"Guides": ["beyond-jsx", "forwarding-refs"]
}
2 changes: 2 additions & 0 deletions pages/docs/manual/latest/import-from-export-to-js.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ canonical: "/docs/manual/latest/import-from-export-to-js"

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.

If you're looking for react-specific interop guidance, check out the [React JS Interop guide](../../react/latest/javascript-interop.mdx).

**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.

In short: **make sure your bindings below output what you'd have manually written in JS**.
Expand Down
55 changes: 55 additions & 0 deletions pages/docs/react/latest/javascript-interop.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: "JavaScript Interop"
description: "Working with React module content in ReScript"
canonical: "/docs/react/latest/javascript-interop"
---

# JavaScript Interop

This section complements the general [Import from / Export to JS](../../manual/latest/import-from-export-to-js.md)
section with React-specific guidance.

## Import from JavaScript

### Importing a JS-based React Component

This is especially useful for 3rd-party components.

<CodeTab labels={["ReScript", "JS Output (ES6)"]}>

```res example
module DatePicker = {
@module("react-datepicker") @react.component
external make: (~date: Js.Date.t, ~onChange: (Js.Date.t) => unit) => React.element = "default"
}

@react.component
let make = () => {
let (date, setDate) = React.useState(Js.Date.make)
<DatePicker date=date onChange={date => setDate(_ => date)} />
}
```
```js
import * as Curry from "./stdlib/curry.js";
import * as React from "react";
import ReactDatepicker from "react-datepicker";

var DatePicker = {};

function Playground(Props) {
var match = React.useState(function () {
return new Date();
});
var setDate = match[1];
return React.createElement(ReactDatepicker, {
date: match[0],
onChange: (function (date) {
return Curry._1(setDate, (function (param) {
return date;
}));
})
});
}
```

</CodeTab>