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
summary: "This is the `@react.componentWithProps` decorator."
6
+
category: "decorators"
7
+
---
8
+
9
+
The `@react.componentWithProps` decorator is used to annotate functions that are [React](/docs/react/latest/elements-and-jsx) components which take in a record type as prop.
10
+
This decorator will ensure your component gets an uppercased name.
11
+
12
+
13
+
> **Note**
14
+
> The `@react.componentWithProps` decorator requires the [react-jsx or jsx config](/docs/react/latest/installation) to be set in your `rescript.json` to enable the required React transformations.
We've created a `Greeting.res` file that contains a `make` function that doesn't receive any props (the function doesn't receive any parameters), and returns a `React.element` that represents `<div> Hello ReScripters! </div>` in the rendered DOM.
53
55
54
-
You can also see in the the JS output that the function we created was directly translated into the pure JS version of a ReactJS component. Note how a `<div>` transforms into a `React.createElement("div",...)` call in JavaScript.
56
+
You can also see in the the JS output that the function we created was directly translated into the pure JS version of a ReactJS component. Note how a `<div>` transforms into a `JsxRuntime.jsx("div",...)` call in JavaScript.
55
57
56
58
## Defining Props
57
59
@@ -272,6 +274,69 @@ The best way to approach this kind of issue is by using props instead of childre
272
274
**The best use-case for `children` is to pass down `React.element`s without any semantic order or implementation details!**
273
275
274
276
277
+
## @react decorators
278
+
279
+
You might've wondered what `@react.component` actually does.
280
+
It's a decorator that tells the ReScript compiler to treat the function as a React component, transforming it at the syntax level.
281
+
282
+
In JavaScript, a React component is just a function that takes props (an object) and returns JSX. In ReScript, props are typically represented as a record type.
283
+
The `@react.component` decorator automatically generates that record type and wraps the function for you—so you don't have to.
However, writing it manually like this means you lose the `make` function name, which prevents JSX from working as expected when using the component elsewhere.
303
+
304
+
Having an uppercased function name also helps distinguish React components from regular functions in [React DevTools](https://react.dev/learn/react-developer-tools).
305
+
306
+
If you prefer defining your own props record, you can use `@react.componentWithProps`. This gives you full control over the props type while still generating a proper uppercased component.
307
+
308
+
<CodeTablabels={["ReScript", "JS Output"]}>
309
+
310
+
```res
311
+
// Counter.res
312
+
type props = {title: string, count: int}
313
+
314
+
@react.componentWithProps
315
+
let make = (props: props) => {
316
+
<h1>
317
+
{React.string(props.title)}
318
+
{React.int(props.count)}
319
+
</h1>
320
+
}
321
+
```
322
+
323
+
```js
324
+
import*asJsxRuntimefrom"react/jsx-runtime";
325
+
326
+
functionCounter(props) {
327
+
returnJsxRuntime.jsxs("h1", {
328
+
children: [
329
+
props.title,
330
+
props.count
331
+
]
332
+
});
333
+
}
334
+
335
+
let make = Counter;
336
+
```
337
+
338
+
</CodeTab>
339
+
275
340
## Props & Type Inference
276
341
277
342
The ReScript type system is really good at inferring the prop types just by looking at its prop usage.
@@ -405,7 +470,7 @@ let content = <Label title="Test"/>
405
470
406
471
## Component Naming
407
472
408
-
Because components are actually a pair of functions, they have to belong to a module to be used in JSX. It makes sense to use these modules for identification purposes as well. `@react.component` automatically adds the name for you based on the module you are in.
473
+
Because components are actually a pair of functions, they have to belong to a module to be used in JSX. It makes sense to use these modules for identification purposes as well. `@react.component`or `@react.componentWithProps`automatically adds the name for you based on the module you are in.
0 commit comments