Skip to content

Instantly share code, notes, and snippets.

@mkhuda
Last active August 31, 2021 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkhuda/c31bd09a5bcb54349b56216ce0138f36 to your computer and use it in GitHub Desktop.
Save mkhuda/c31bd09a5bcb54349b56216ce0138f36 to your computer and use it in GitHub Desktop.
React.useRef to handle ReactDOMRe.domRef
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