Skip to content

Commit ec753ff

Browse files
committed
docs - add js output to module function example
1 parent f657933 commit ec753ff

File tree

1 file changed

+75
-23
lines changed

1 file changed

+75
-23
lines changed

pages/docs/manual/latest/module-functions.mdx

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,87 @@ canonical: "/docs/manual/latest/module-functions"
66

77
# Module Functions
88

9-
```rescript
10-
// next.res - bindings for Next
11-
module type Params = {
12-
type t
13-
}
9+
<CodeTab labels={["ReScript", "JS Output"]}>
10+
```res example
11+
module Next = {
12+
// define a module type to use a parameter for out module function
13+
module type Params = {
14+
type t
15+
}
1416
15-
module MakeNavigation = (Params: Params) => {
16-
@module("next/navigation")
17-
external useNavigation: unit => Params.t = "useNavigation"
17+
// define our module function
18+
module MakeNavigation = (Params: Params) => {
19+
@module("next/navigation")
20+
external useNavigation: unit => Params.t = "useNavigation"
21+
/* You can use values from the function parameter, such as Params.t */
22+
}
1823
}
1924
20-
// Your React component
21-
// component.res
22-
module Params = {
23-
type t = {
24-
tag: string,
25-
item: string,
25+
module Component: {
26+
@react.component
27+
let make: unit => Jsx.element
28+
} = {
29+
// Create a module that matches the module type expected by Next.MakeNavigation
30+
module Params = {
31+
type t = {
32+
tag: string,
33+
item: string,
34+
}
2635
}
36+
37+
// Create a new module using the Params module we created and the Next.MakeNavigation module function
38+
module Navigation = Next.MakeNavigation(Params)
39+
40+
@react.component
41+
let make = () => {
42+
// Use the functions, values, or types created by the module function
43+
let params = Navigation.useNavigation()
44+
<div>
45+
<p>
46+
{React.string("Tag: " ++ params.tag /* params is fully typed! */)}
47+
</p>
48+
<p> {React.string("Item: " ++ params.item)} </p>
49+
</div>
50+
}
51+
}
52+
```
53+
```js
54+
// Generated by ReScript, PLEASE EDIT WITH CARE
55+
56+
import * as $$Navigation from "next/navigation";
57+
import * as JsxRuntime from "react/jsx-runtime";
58+
59+
function MakeNavigation(Params) {
60+
return {};
2761
}
2862

29-
module Navigation = Next.MakeNavigation(Params)
63+
var Next = {
64+
MakeNavigation: MakeNavigation
65+
};
3066

31-
@react.component
32-
let make = () => {
33-
let params = Navigation.useNavigation()
34-
<div>
35-
<p> {React.string("Tag: " ++ params.tag)} </p>
36-
<p> {React.string("Item: " ++ params.item)} </p>
37-
</div>
67+
function Playground$Component(props) {
68+
var params = $$Navigation.useNavigation();
69+
return JsxRuntime.jsxs("div", {
70+
children: [
71+
JsxRuntime.jsx("p", {
72+
children: "Tag: " + params.tag
73+
}),
74+
JsxRuntime.jsx("p", {
75+
children: "Item: " + params.item
76+
})
77+
]
78+
});
3879
}
80+
81+
var Component = {
82+
make: Playground$Component
83+
};
84+
85+
export {
86+
Next ,
87+
Component ,
88+
}
89+
/* next/navigation Not a pure module */
90+
3991
```
40-
import { comma } from "postcss/lib/list"
92+
</ CodeTab>

0 commit comments

Comments
 (0)