useRefInit
External Documentation
Ref<
Returns a mutable Ref object with Ref.current property initialized to
initialValue.
Changes to the Ref.current property do not cause the containing
function component to re-render.
The returned Ref object will persist for the full lifetime of the
function component. Compare to createRef which returns a new Ref
object on each render.
Note: there are two rules for using Hooks:
- Only call Hooks at the top level.
- Only call Hooks from inside a function component.
Example:
final MyComponent = registerFunctionComponent((props) {
final countRef = useRefInit(1);
void handleClick([_]) {
countRef.current = countRef.current + 1;
print('You clicked ${countRef.current} times!');
}
return button(text: 'Click me!', onClick: handleClick);
});
Implementation
Ref<T> useRefInit<T>(T initialValue) {
final jsInitial = _toJsAny(initialValue);
final jsRef = React.useRef(jsInitial);
return Ref<T>.fromJs(JsRef.fromJs(jsRef));
}