February 2019
Beginner to intermediate
180 pages
4h 4m
English
The React ref is just an instance variable of state:
type state = { isOpen: bool, touches, width: ref(float),};
We attach the React ref on the <nav /> element by setting the ref property to the result of self.handle((ref, self) => ...):
ref={ self.handle((ref, self) => self.state.width := ( switch (Js.Nullable.toOption(ref)) { | None => 0.0 | Some(r) => ReactDOMRe.domElementToObj(r)##clientWidth } ) )}
Since a React ref could be null in JavaScript, we convert it to an option and pattern match on its value.
The type of the React ref depends on whether it is a DOM element or a React component. The former's type is Dom.element, and the latter's is ReasonReact.reactRef. To convert a ReasonReact.reactRef to a JavaScript object, use ...
Read now
Unlock full access