Skip to content
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

TypeError: undefined is not an object (evaluating '(i === 0 ? obj : resVal)[pathArray[i]]') #142

Open
arjunlandes opened this issue Apr 19, 2020 · 4 comments

Comments

@arjunlandes
Copy link

I'm basically using the demo Gist verbatim:

https://snack.expo.io/@almouro/react-native-formik-gist

When I type a single letter into any text field I get the error:

TypeError: undefined is not an object (evaluating '(i === 0 ? obj : resVal)[pathArray[i]]')

I tried removing validationSchema and I still got the error. Separately, I tried removing withNextInputAutoFocusForm and withNextInputAutoFocusInput, and still got the problem. It feels like a bug in handleTextInput. Maybe I'm doing something stupid in the syntax below.

From package.json:

"formik": "^2.1.4",
"react-native-formik": "^1.7.8",
"react-native-material-textfield": "^0.16.1",
"recompose": "^0.30.0",
"yup": "^0.28.3"

Code:

import React from 'react';
import { Button, ScrollView, View } from 'react-native';
import { compose } from 'recompose';
import { Formik } from 'formik';
import * as Yup from 'yup';
import {
  handleTextInput,
  withNextInputAutoFocusForm,
  withNextInputAutoFocusInput
} from 'react-native-formik';
import { OutlinedTextField } from 'react-native-material-textfield';

const MyInput = compose(
  handleTextInput,
  withNextInputAutoFocusInput
)(OutlinedTextField);
const Form = withNextInputAutoFocusForm(View);

const validationSchema = Yup.object().shape({
  country: Yup.string()
    .required('Required.')
    .length(2, 'Must be 2 letter country code.'),
});

const RegistrationForm = props => (
  <Formik
    onSubmit={values => console.log(values)}
    validationSchema={validationSchema}
  >
    {props => (
      <Form>
        <MyInput label="Country" name="country" type="name"/>
        <MyInput label="Business name" name="name" type="name"/>
        <MyInput label="Business website" name="website" type="email"/>
        <MyInput label="Business phone number" name="phone" type="digits"/>
        <MyInput label="Address line 1" name="addressLine1" type="name"/>
        <MyInput label="Address line 2" name="addressLine2" type="name"/>
        <MyInput label="City" name="city" type="name"/>
        <MyInput label="State" name="state" type="name"/>
        <MyInput label="Zip code" name="zip" type="digits"/>
        <Button onPress={props.handleSubmit} title="SUBMIT" />
      </Form>
    )}
  </Formik>
);

export default class MyScreen extends React.Component {
  render() {
    return (
      <ScrollView style={{ padding: 30 }}>
        <RegistrationForm/>
      </ScrollView>
    );
  }
}
@arjunlandes
Copy link
Author

Ping. It actually must be a problem with withNextInputAutoFocusForm or withNextInputAutoFocusInput, since handleTextInput works fine on its own.

@alkalox
Copy link

alkalox commented Jun 15, 2020

Edit: Was getting the same bug. Seems like this issue is arising because react-native-formik hasn't been updated for Formik v2. react-native-formik currently works well with [email protected] and all the other versions before that.

Downgrading Formik to 1.5.8 got it working :)

@Ripplz
Copy link

Ripplz commented Oct 20, 2020

I use this library with Formik v2.1.5 on my React Native app (Expo), and it works just fine.

From the error message, I think this has to do with an upgrade made in Formik v2^, which requires you to provide a value for each of your form elements in an object passed to your Formik component's initialValues prop.

Try this and see if it would work.

Originally posted by @Ripplz in #146 (comment)

@Ripplz
Copy link

Ripplz commented Oct 20, 2020

Okay, I've successfully reproduced this error in my Formik v2+ app, and I can confirm it's what I mentioned above. You don't need to downgrade your Formik, just include the initalValues prop on your <Formik> component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants