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/manual/v11.0.0/mutation.mdx
+44
Original file line number
Diff line number
Diff line change
@@ -73,3 +73,47 @@ Note that the previous binding `five` stays `5`, since it got the underlying ite
73
73
## Tip & Tricks
74
74
75
75
Before reaching for `ref`, know that you can achieve lightweight, local "mutations" through [overriding let bindings](let-binding.md#binding-shadowing).
76
+
77
+
### Self-Referencing Assignments
78
+
79
+
There are scenarios where using a mutable `ref` binding becomes necessary, particularly when dealing with self-referencing. This is common when working with functions that return a value and accept a callback that uses that return value. In such cases, you need to initialize a mutable `ref` binding and then assign to it. Here's an example:
80
+
81
+
<CodeTablabels={["ReScript", "JS Output"]}>
82
+
83
+
```res example
84
+
// hypothetic "showDialog" function shows the given `~content: JSX.element`
85
+
// in the dialog. It returns a cleanup function that closes/removes the dialog.
86
+
@val external showDialog: (~content: Jsx.element) => unit => unit = "showDialog"
87
+
88
+
// We want render a "Close" button in the `~content` that will call the
89
+
// returned cleanup function when clicked. This requires a mutable binding.
90
+
// First initialize it with a dummy function that has the same signature as
91
+
// our cleanup function.
92
+
let cleanupDialog = ref(() => ())
93
+
94
+
// assign to the ref and self-refer to the value in the event handler
0 commit comments