I have a reusable custom input component that may or may not be used in a Formik context (i.e. either in a Formik form or outside of it generally). The behavior needs to be different accordingly. I need to determine the mode in this component. There is a hook called useFormikContext() that will throw an error if it's not applicable. But when I do
try {
const { values, setFieldValue } = useFormikContext();
}
catch (error) {
alert('NOT in the Formik Context');
}
finally {
alert ('End');
}
I get
Line 17:43: React Hook "useFormikContext" is called conditionally.
React Hooks must be called in the exact same order in every component render react-hooks/rules-of-hooks
So is there a way to figure out cleanly whether I can apply the hook or not?