You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/docs/react/latest/arrays-and-keys.mdx
+5-3Lines changed: 5 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ Whenever we are transforming data into an array of elements and put it in our Re
15
15
16
16
## Keys & Rendering Arrays
17
17
18
-
Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity:
18
+
Keys help React identify which elements have been changed, added, or removed throughout each render. Keys should be given to elements inside the array to give the elements a stable identity:
19
19
20
20
```res
21
21
let numbers = [1, 2, 3, 4, 5];
@@ -40,7 +40,7 @@ let items = Belt.Array.map(todos, todo => {
40
40
})
41
41
```
42
42
43
-
When you don’t have stable IDs for rendered items, you may use the item index as a key as a last resort:
43
+
If you don’t have stable IDs for rendered items, you may use the item index as a key as a last resort:
44
44
45
45
```res {2,3}
46
46
let items = Belt.Array.mapWithIndex(todos, (todo, i) => {
@@ -119,5 +119,7 @@ let items =
119
119
<div> {React.array(items)} </div>
120
120
```
121
121
122
-
We used `Belt.List.toArray` to convert our list to an array before creating our `array<React.element>`. Please note that using `list` has a performance impact due to extra conversion costs.
122
+
We use `Belt.List.toArray` to convert our list to an array before creating our `array<React.element>`. Please note that using `list` has performance impact due to extra conversion costs.
123
+
124
+
In 99% you'll want to use arrays (seamless interop, faster JS code), but in some cases it might make sense to use a `list` to leverage advanced pattern matching features etc.
Copy file name to clipboardExpand all lines: pages/docs/react/latest/components-and-props.mdx
+17-7Lines changed: 17 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -143,7 +143,13 @@ var greeting = React.createElement(Playground$Greeting, tmp);
143
143
144
144
</CodeTab>
145
145
146
-
### Children Props
146
+
### Special Props `key` and `ref`
147
+
148
+
You can't define any props called `key` or `ref`. React treats those props differently and the compiler will will yield an error whenever you try to define a `~key` or `~ref` argument in your component function.
149
+
150
+
Check out the corresponding [Arrays and Keys](./arrays-and-keys) and [Forwarding React Refs](./forwarding-refs) sections for more details.
151
+
152
+
## Children Props
147
153
148
154
In React `props.children` is a special attribute to represent the nested elements within a parent element:
149
155
@@ -250,17 +256,18 @@ module NoChildren = {
250
256
<NoChildren> <div/> </NoChildren>
251
257
```
252
258
253
-
Children props are really tempting to be abused as a way to model hierarchies, e.g. `<List> <ListHeader/> <Item/> </List>` (`List` should only allow `Item` / `ListHeader` elements), but this kind of constraint is hard to enforce because all components end up being `React.element`, so it would require notorious runtime checking within `List` to verify that all children are in fact of type `Item` or `ListHeader`.
259
+
Children props are really tempting to be abused as a way to model hierarchies, e.g. `<List> <ListHeader/> <Item/> </List>` (`List` should only allow `Item` / `ListHeader` elements), but this kind of constraint is hard to enforce because all components end up being a `React.element`, so it would require notorious runtime checking within `List` to verify that all children are in fact of type `Item` or `ListHeader`.
254
260
255
-
The best way to approach this kind of issue is by using props instead of children, e.g. `<List header="..." items=[{id: "...", text: "..."}]/>`. This way it's easy to type check the constraints, and also spares us many hours debugging and remembering component constraints.
261
+
The best way to approach this kind of issue is by using props instead of children, e.g. `<List header="..." items=[{id: "...", text: "..."}]/>`. This way it's easy to type check the constraints, and it also spares component consumers from memorizing and remembering component constraints.
256
262
257
-
**The best use-case for `children` is to pass down `React.element`s, no matter what kind of elements they are!**
263
+
**The best use-case for `children` is to pass down `React.element`s without any semantic order or implementation details!**
258
264
259
265
266
+
## Props & Type Inference
260
267
261
-
### Type Inference
268
+
The ReScript type system is really good at inferring the prop types just by looking at its prop usage.
262
269
263
-
The ReScript type system excels at computing the actual prop types through usage. We recommend to explicitly type your props (especially for exported components) to help your coworkers understand the types. For simple casesor experimentation, it's still fine to omit them:
270
+
For simple cases, well-scoped usage, or experimentation, it's still fine to omit type annotations:
264
271
265
272
266
273
```res
@@ -275,8 +282,9 @@ let make = (~onClick, ~msg, ~children) => {
275
282
}
276
283
```
277
284
278
-
In the example above, `onClick` will be inferred as `ReactEvent.Mouse.t => unit`, `msg` as `string` and `children` as `React.element`. Type inference is very useful to ommit a lot of annoying type annotations, especially for private functions that pass around values to other functions.
285
+
In the example above, `onClick` will be inferred as `ReactEvent.Mouse.t => unit`, `msg` as `string` and `children` as `React.element`. Type inference is especially useful when you just forward values to some smaller (privately scoped) functions.
279
286
287
+
Even tough type inference spares us a lot of keyboard typing, we still recommend to explicitly type your props (just like with any public API) for better type visibility and to prevent confusing type errors.
280
288
281
289
## Using Components in JSX
282
290
@@ -309,6 +317,8 @@ var make = App;
309
317
310
318
**Note:** React components are capitalized; primitive DOM elements like `div` or `button` are uncapitalized. More infos on the JSX specifics and code transformations can be found in our [JSX section](/docs/manual/latest/jsx#capitalized-tag) in our language manual.
311
319
320
+
More details on the `@react.component` decorator and its generated interface can be found in our [Advanced React JSX Transformation](./react-jsx-transformation) page.
Copy file name to clipboardExpand all lines: pages/docs/react/latest/introduction.mdx
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,8 @@ ReScript offers first class bindings for [ReactJS](https://reactjs.org) and are
11
11
12
12
The ReScript philosophy is to compile as closely to idiomatic JS code as possible; in case of ReactJS we made no exception, so it's not only easy to transfer all the React knowledge to the ReScript platform, but also straightorward to integrate with existing ReactJS codebases and libraries.
13
13
14
+
All our documentated examples can be compiled in our [ReScript Playground](/try) as well.
15
+
14
16
> **This documentation assumes basic knowledge about ReactJS.**
15
17
>
16
18
> Please note that even though we will cover many basic React concepts, it might still be necessary to have a look at the official [ReactJS](https://reactjs.org) resources, especially if you are a complete beginner with React.
**Note:** This is an escape hatch feature and will only be useful for interoping with existing JS code / libraries.
130
129
131
-
Sometimes it's required to clone an existing element to set overwrite prop values or add props to a new instance. You can use `React.cloneElement` for that:
130
+
Sometimes it's required to clone an existing element to set, overwrite or add prop values to a new instance, or if you want to set invalid prop names such as `data-name`. You can use `React.cloneElement` for that:
132
131
133
132
<CodeTablabels={["ReScript", "JS Output"]}>
134
133
135
134
```res
136
135
let original = <div className="hello"/>
137
136
138
137
// Will return a new React.element with className set to "world"
@@ -154,7 +154,6 @@ The feature mentioned above could also replicate `props spreading`, a practise c
154
154
155
155
In ReScript, we rather pass down required props explicitly to leaf components or use a renderProp instead. We introduced [JSX punning](/docs/manual/latest/jsx#punning) syntax to make the process of passing down props more convenient.
156
156
157
-
158
157
## Using Elements within JSX
159
158
160
159
You can compose elements into more complex structures by using JSX:
0 commit comments