I am creating a form using Formik. I have created my form following the pattern here: https://formik.org/docs/examples/basic
Now I want to use the result of the useParams react hook (https://reach.tech/router/api/useParams) as an input to the onSubmit function.
This is the onSubmit part from the Formik docs:
onSubmit={async (values) => { await new Promise((r) => setTimeout(r, 500)); alert(JSON.stringify(values, null, 2)); }} I tried adding this line:
onSubmit={async (values) => { await new Promise((r) => setTimeout(r, 500)); const myValue = useParams()["myParam"] alert(JSON.stringify(values, null, 2)); }} where useParams is imported from 'react-router-dom'
But this gives me an error: React Hook "useParams" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React Hook function
I am new to React/Formik and don't know how to proceed from here. How can I get the value of myParam inside the onSubmit function?
const myValue = useParams()["myParam"]? Please show the full code snippet for this part. That would help in narrowing down your problem