-
-
Notifications
You must be signed in to change notification settings - Fork 262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing React Refs To The Hook #1015
Comments
@cutterbl hi, now it's possible to pass |
This is a fantastic start. Your change gives the developer a way to access the inner inputRef (ref), but it does not yet allow for access to the maskRef. Functionally this will work. That said, my understanding is that it is bad practice to use a hook outside of the function itself (meaning using it in the method signature). I may be wrong here, but I think this will throw ESLint errors when using the Rules of Hooks plugin. Currently you have import IMask, { type InputMask, type InputMaskElement, type FactoryOpts } from 'imask';
import { useEffect, useCallback, useState, useRef, Dispatch } from 'react';
import type { MutableRefObject } from 'react';
export default
function useIMask<
MaskElement extends InputMaskElement,
Opts extends FactoryOpts=FactoryOpts,
>(
opts: Opts,
{ onAccept, onComplete, ref=useRef<MaskElement | null>(null) }: {
ref?: MutableRefObject<MaskElement | null>,
onAccept?: (value: InputMask<Opts>['value'], maskRef: InputMask<Opts>, e?: InputEvent) => void;
onComplete?: (value: InputMask<Opts>['value'], maskRef: InputMask<Opts>, e?: InputEvent) => void;
} = {}
): {
ref: MutableRefObject<MaskElement | null>,
maskRef: MutableRefObject<InputMask<Opts> | null>,
value: InputMask<Opts>['value'],
setValue: Dispatch<InputMask<Opts>['value']>,
unmaskedValue: InputMask<Opts>['unmaskedValue'],
setUnmaskedValue: Dispatch<InputMask<Opts>['unmaskedValue']>,
typedValue: InputMask<Opts>['typedValue'],
setTypedValue: Dispatch<InputMask<Opts>['typedValue']>,
} {
const maskRef = useRef<InputMask<Opts> | null>(null);
// ... And this works. But supposedly this (which also works) would be considered the proper way to do this in React. import IMask, { type InputMask, type InputMaskElement, type FactoryOpts } from 'imask';
import { useEffect, useCallback, useState, , useRef, createRef, Dispatch } from 'react';
import type { MutableRefObject } from 'react';
export default
function useIMask<
MaskElement extends InputMaskElement,
Opts extends FactoryOpts=FactoryOpts,
>(
opts: Opts,
{ onAccept, onComplete, ref=createRef<MaskElement | null>(null) }: {
ref?: MutableRefObject<MaskElement | null>,
onAccept?: (value: InputMask<Opts>['value'], maskRef: InputMask<Opts>, e?: InputEvent) => void;
onComplete?: (value: InputMask<Opts>['value'], maskRef: InputMask<Opts>, e?: InputEvent) => void;
} = {}
): {
ref: MutableRefObject<MaskElement | null>,
maskRef: MutableRefObject<InputMask<Opts> | null>,
value: InputMask<Opts>['value'],
setValue: Dispatch<InputMask<Opts>['value']>,
unmaskedValue: InputMask<Opts>['unmaskedValue'],
setUnmaskedValue: Dispatch<InputMask<Opts>['unmaskedValue']>,
typedValue: InputMask<Opts>['typedValue'],
setTypedValue: Dispatch<InputMask<Opts>['typedValue']>,
} {
const maskRef = useRef<InputMask<Opts> | null>(null);
// ... |
@cutterbl You suggest using |
Absolutely see your point, about My thoughts on the |
Sometimes you're creating your own general custom components, wrapping
IMaskInput
. In instances like these, where one is using their own custom as a base, the developer may want to pass refs to the component, rather than having the one's created by the hook.For instance, say your custom wrapper uses it's own hook that builds out generic
onAccept
andonComplete
methods, as well as separating out input properties from mask options. And, say your component props can include their ownref
andinputRef
. Then a developer does something like:Currently the code for the
useIMask
hook creates these refs for the hook, and doesn't allow you to pass in refs. You could default the options in the hook method signature{ ref = React.createRef(), inputRef = React.createRef(), ...otherOpts }
. By doing this, you allow the developer more flexibility in how they build their components.The text was updated successfully, but these errors were encountered: