Skip to content

Commit 65c73d3

Browse files
committed
Use CodeTab
1 parent 8edb9a9 commit 65c73d3

File tree

1 file changed

+80
-13
lines changed

1 file changed

+80
-13
lines changed

pages/docs/react/latest/interopt.mdx renamed to pages/docs/react/latest/interop.mdx

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,12 @@ including defining component props and handling various import scenarios.
1414

1515
To reuse a React component in ReScript, create a new module, specify the component's location, and define its props.
1616

17-
```js
18-
import Confetti from "react-confetti"
19-
20-
const App = () => {
21-
<Confetti width={300} height={300} />
22-
}
23-
```
17+
<CodeTab labels={["ReScript", "JS Output"]}>
2418

2519
```res
2620
module Confetti = {
27-
@module("react-confetti")
28-
@react.component
29-
external make : (~width:int, ~height: int) => React.element = "default"
21+
@module("react-confetti") @react.component
22+
external make: (~width: int, ~height: int) => React.element = "default"
3023
}
3124
3225
// Assuming we are in App.res
@@ -36,28 +29,49 @@ let make = () => {
3629
}
3730
```
3831

32+
```js
33+
import ReactConfetti from "react-confetti";
34+
import * as JsxRuntime from "react/jsx-runtime";
35+
36+
var Confetti = {};
37+
38+
function Playground(props) {
39+
return JsxRuntime.jsx(ReactConfetti, {
40+
width: 300,
41+
height: 300
42+
});
43+
}
44+
```
45+
46+
</CodeTab>
47+
3948
## Importing from Relative Paths
4049

4150
You can import components from relative file paths using the `@module` attribute.
4251
Use "default" to indicate the default export, or specify a named export if needed.
4352

4453
### Named Export Example
4554

55+
<CodeTab labels={["ReScript"]}>
56+
4657
```res
4758
// Equivalent of import { Foo } from "bar"
4859
module Foo = {
49-
@module("bar")
50-
@react.component
51-
external make : () => React.element = "Foo"
60+
@module("bar") @react.component
61+
external make: unit => React.element = "Foo"
5262
}
5363
```
5464

65+
</CodeTab>
66+
5567
## Defining Props Types
5668

5769
You can define a separate type for your component's props within the module.
5870

5971
### Props Type Example
6072

73+
<CodeTab labels={["ReScript", "JS Output"]}>
74+
6175
```res
6276
module Confetti = {
6377
type confettiProps = {
@@ -75,10 +89,28 @@ let make = () => {
7589
}
7690
```
7791

92+
```js
93+
import ReactConfetti from "react-confetti";
94+
import * as JsxRuntime from "react/jsx-runtime";
95+
96+
var Confetti = {};
97+
98+
function Playground(props) {
99+
return JsxRuntime.jsx(ReactConfetti, {
100+
width: 300,
101+
height: 300
102+
});
103+
}
104+
```
105+
106+
</CodeTab>
107+
78108
## Optional Props
79109

80110
To define optional props, use the `?` symbol.
81111

112+
<CodeTab labels={["ReScript", "JS Output"]}>
113+
82114
```res
83115
module Confetti = {
84116
type confettiProps = {
@@ -98,10 +130,28 @@ let make = () => {
98130
}
99131
```
100132

133+
```js
134+
import ReactConfetti from "react-confetti";
135+
import * as JsxRuntime from "react/jsx-runtime";
136+
137+
var Confetti = {};
138+
139+
function Playground(props) {
140+
return JsxRuntime.jsx(ReactConfetti, {
141+
width: 300,
142+
height: 300
143+
});
144+
}
145+
```
146+
147+
</CodeTab>
148+
101149
## Extending Built-in DOM Nodes
102150

103151
To accept existing DOM props for a component, extend the `JsxDOM.domProps` type.
104152

153+
<CodeTab labels={["ReScript", "JS Output"]}>
154+
105155
```res
106156
module Foo = {
107157
type fooProps = {
@@ -119,4 +169,21 @@ let make = () => {
119169
}
120170
```
121171

172+
```js
173+
import Foo from "foo";
174+
import * as JsxRuntime from "react/jsx-runtime";
175+
176+
var Foo$1 = {};
177+
178+
function Playground(props) {
179+
return JsxRuntime.jsx(Foo, {
180+
height: "300px",
181+
width: "300px",
182+
customProp: "bar"
183+
});
184+
}
185+
```
186+
187+
</CodeTab>
188+
122189
In this example `width` and `height` can be set because `JsxDOM.domProps` was spread into `fooProps`.

0 commit comments

Comments
 (0)