Skip to content

Commit c68d381

Browse files
committed
Add React interopt guide
1 parent 6eb48c0 commit c68d381

File tree

2 files changed

+124
-1
lines changed

2 files changed

+124
-1
lines changed

data/sidebar_react_latest.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"Guides": [
2929
"beyond-jsx",
3030
"forwarding-refs",
31-
"extensions-of-props"
31+
"extensions-of-props",
32+
"interopt"
3233
]
3334
}

pages/docs/react/latest/interopt.mdx

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
---
2+
title: Interopt
3+
description: "Reusing existing React components"
4+
canonical: "/docs/react/latest/interopt"
5+
---
6+
7+
# Interopt
8+
9+
Reusing existing React components in ReScript is straightforward.
10+
This guide will walk you through the steps required to import and use React components within ReScript,
11+
including defining component props and handling various import scenarios.
12+
13+
## Basic Example
14+
15+
To reuse a React component in ReScript, create a new module, specify the component's location, and define its props.
16+
17+
```js
18+
import Confetti from "react-confetti"
19+
20+
const App = () => {
21+
<Confetti width={300} height={300} />
22+
}
23+
```
24+
25+
```res
26+
module Confetti = {
27+
@module("react-confetti")
28+
@react.component
29+
external make : (~width:int, ~height: int) => React.element = "default"
30+
}
31+
32+
// Assuming we are in App.res
33+
@react.component
34+
let make = () => {
35+
<Confetti width={300} height={300} />
36+
}
37+
```
38+
39+
## Importing from Relative Paths
40+
41+
You can import components from relative file paths using the `@module` attribute.
42+
Use "default" to indicate the default export, or specify a named export if needed.
43+
44+
### Named Export Example
45+
46+
```res
47+
// Equivalent of import { Foo } from "bar"
48+
module Foo = {
49+
@module("bar")
50+
@react.component
51+
external make : () => React.element = "Foo"
52+
}
53+
```
54+
55+
## Defining Props Types
56+
57+
You can define a separate type for your component's props within the module.
58+
59+
### Props Type Example
60+
61+
```res
62+
module Confetti = {
63+
type confettiProps = {
64+
width: int,
65+
height: int,
66+
}
67+
68+
@module("react-confetti") @react.component(: confettiProps)
69+
external make: confettiProps => React.element = "default"
70+
}
71+
72+
@react.component
73+
let make = () => {
74+
<Confetti width={300} height={300} />
75+
}
76+
```
77+
78+
## Optional Props
79+
80+
To define optional props, use the `?` symbol.
81+
82+
```res
83+
module Confetti = {
84+
type confettiProps = {
85+
width: int,
86+
height: int,
87+
initialVelocityX?: int,
88+
initialVelocityY?: int,
89+
}
90+
91+
@module("react-confetti") @react.component(: confettiProps)
92+
external make: confettiProps => React.element = "default"
93+
}
94+
95+
@react.component
96+
let make = () => {
97+
<Confetti width={300} height={300} />
98+
}
99+
```
100+
101+
## Extending Built-in DOM Nodes
102+
103+
To accept existing DOM props for a component, extend the `JsxDOM.domProps` type.
104+
105+
```res
106+
module Foo = {
107+
type fooProps = {
108+
...JsxDOM.domProps,
109+
customProp: string,
110+
}
111+
112+
@module("foo") @react.component(: fooProps)
113+
external make: fooProps => React.element = "default"
114+
}
115+
116+
@react.component
117+
let make = () => {
118+
<Foo width={"300px"} height={"300px"} customProp="bar" />
119+
}
120+
```
121+
122+
In this example `width` and `height` can be set because `JsxDOM.domProps` was spread into `fooProps`.

0 commit comments

Comments
 (0)