Last active
August 31, 2021 07:29
-
-
Save mkhuda/c31bd09a5bcb54349b56216ce0138f36 to your computer and use it in GitHub Desktop.
React.useRef to handle ReactDOMRe.domRef
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
React.useRef to handle ReactDOMRe.domRef | |
Here are a few solutions: | |
```ocaml | |
let inputRef = React.createRef(); | |
<input ref={ReactDOMRe.Ref.domRef(inputRef)} type="text" /> | |
``` | |
If React.createRef is not available in your current version, simply use: | |
```ocaml | |
let inputRef = React.useRef(Js.Nullable.null); | |
<input ref={ReactDOMRe.Ref.domRef(inputRef)} type="text" /> | |
``` | |
In case that you want to convert Js.Nullable.t to option beforehand: | |
```ocaml | |
let inputRef = React.useRef(None); | |
<input ref=ref={ReactDOMRe.Ref.callbackDomRef(elem => | |
React.Ref.setCurrent(inputRef, Js.Nullable.toOption(elem)) | |
)} type="text" /> | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment