32

Some info

I am using Formik for my project and I have my setup looking like this:

|-MenuModal
|--MenuEdit
|---MenuEditForm

Where MenuModal is the parent to MenuEdit and MenuEditForm. The component MenuEditForm is responsible for returning the Formik form, but I am calling the submit in it's parent MenuModal, which laters runs the submit function in MenuEdit via React's refs. Messy? Yup!

My problem

Right now I am trying to use state and callback functions to get the Formiks values from MenuEditForm to MenuEdit. But since I am not using Formiks own onSubmit:

    <Formik
        initialValues={menu}
        validationSchema={validationSchema}
        onSubmit={values => console.log('values', values)} // 'values' is undefined
        ...

My values will be undefined and I can't make my submit function go through.

So I wonder how I can access my values in MenuEditForm so I later can pass it up to MenuEdit and complete my submit function.

Thanks for reading.

4 Answers 4

64

To access values outside of the formik component, you can do this with hooks:

      const formRef = useRef();

      return (
        <Formik
          ...
          innerRef={formRef}
        >
        ...
      </Formik>

Then, can access values using formRef.current.values anywhere outside of the component.

Sign up to request clarification or add additional context in comments.

4 Comments

Any idea why the innerRef prop is nowhere to be found in the docs?
@AbrahamL That's for Field.
This works, I can use handleChange in useEffect.
3

Since formik passes value to onChangeText we can save it in useState for dynamic updates

  onChangeText={(value: string) => {
      handleChange('name')(value);
      setName(value);
  }}

Comments

2

In my case (using older version of react 16.5) the "innerRef" prop does not exist so I used the regular "ref" and it worked.

Here an ex with class component:

class MyComponent extends Component {
    constructor(props) {
        super(props);
        this.state = {
        };
        this.formRef = React.createRef();
    }

Before using it you should check that the Formik class is accessible because it is null when the page is rendering :

getFormikValues = () => {
    const formikValues = this.formRef.current && this.formRef.current.state && 
    this.formRef.current.state.values
    return formikValues
}

or if you have a support for optional chaining

(see More supported alternative to optional chaining in React?)

just use:

   getFormikValues = () => {
      const formikValues = this.formRef.current?.state?.values
       return formikValues
   }

  doSomethingWithFormikValues = () => {
    const formikValues = this.getFormikValues()
    return
  }

  return (
    <Formik
      ref={this.formRef}
    >
    ...
  </Formik>

Comments

0

You can simply pass a function to receive the values from child component.

Eg:

MenuModal:

const MenuModal = () => {
  const [values, setvalues] = useState();
  return (
    ...
      <MenuEdit values={values} onFormSubmit={(values) => setvalues(values)} />
    ...
  );
}

MenuEdit:

const MenuEdit = ({values, onFormSubmit}) => {
    // do something with values
    return (
      ...
      <MenuEditForm onFormSubmit={onFormSubmit} />
      ...
    )
  ...  
}

MenuEditForm:

const MenuEditForm = ({onFormSubmit}) => (
  ...
  <Formik
    initialValues={menu}
    validationSchema={validationSchema}
    onSubmit={values => onFormSubmit(values)}
  ...
)

1 Comment

What if you need access to the values before the form submits?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.