Formik: What is the current way of defining the ref for an innerRef attribute using TypeScript?

Created on 14 Feb 2020  路  4Comments  路  Source: formium/formik

鉂換uestion

Currently the innerRef has signature of (instance: any) => void which does not fit any type I can remind of, like FormikProps or FormikHelpers

Before that when I was using the 1.5 version where I could call methods like setFieldTouched on the ref therefore an inner Ref should be of type FormikHelper or at least inherit it. The (instance: any) => void looks broken

stale

All 4 comments

Since TypeScript is not really helpful i used as casting.

<Formik innerRef={instance=>{formikRef=instance}} {...otherProps}/>`

This however makes a non-constant formikRef variable

let formikRef = useRef(null) as any;

I do not think this is correct way with 2.x

We do currently use any. You are free to open a PR to change the innerRef to the signature that I wrote for useRef here: #2222

TypeError: formikRef.setFieldTouched is not a function

@ProteanCode are you using innerRef? My PR using ref itself is a draft, but you can take the same types I used to implement useRef to open a PR for innerRef which, if accepted, should solve your issue with any.

Was this page helpful?
0 / 5 - 0 ratings