@@ -14,19 +14,12 @@ including defining component props and handling various import scenarios.
14
14
15
15
To reuse a React component in ReScript, create a new module, specify the component's location, and define its props.
16
16
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" ]} >
24
18
25
19
``` res
26
20
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"
30
23
}
31
24
32
25
// Assuming we are in App.res
@@ -36,28 +29,49 @@ let make = () => {
36
29
}
37
30
```
38
31
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
+
39
48
## Importing from Relative Paths
40
49
41
50
You can import components from relative file paths using the ` @module ` attribute.
42
51
Use "default" to indicate the default export, or specify a named export if needed.
43
52
44
53
### Named Export Example
45
54
55
+ <CodeTab labels = { [" ReScript" ]} >
56
+
46
57
``` res
47
58
// Equivalent of import { Foo } from "bar"
48
59
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"
52
62
}
53
63
```
54
64
65
+ </CodeTab >
66
+
55
67
## Defining Props Types
56
68
57
69
You can define a separate type for your component's props within the module.
58
70
59
71
### Props Type Example
60
72
73
+ <CodeTab labels = { [" ReScript" , " JS Output" ]} >
74
+
61
75
``` res
62
76
module Confetti = {
63
77
type confettiProps = {
@@ -75,10 +89,28 @@ let make = () => {
75
89
}
76
90
```
77
91
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
+
78
108
## Optional Props
79
109
80
110
To define optional props, use the ` ? ` symbol.
81
111
112
+ <CodeTab labels = { [" ReScript" , " JS Output" ]} >
113
+
82
114
``` res
83
115
module Confetti = {
84
116
type confettiProps = {
@@ -98,10 +130,28 @@ let make = () => {
98
130
}
99
131
```
100
132
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
+
101
149
## Extending Built-in DOM Nodes
102
150
103
151
To accept existing DOM props for a component, extend the ` JsxDOM.domProps ` type.
104
152
153
+ <CodeTab labels = { [" ReScript" , " JS Output" ]} >
154
+
105
155
``` res
106
156
module Foo = {
107
157
type fooProps = {
@@ -119,4 +169,21 @@ let make = () => {
119
169
}
120
170
```
121
171
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
+
122
189
In this example ` width ` and ` height ` can be set because ` JsxDOM.domProps ` was spread into ` fooProps ` .
0 commit comments