Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

javascript - Formik validation calling hooks outside a function

I am trying to use Formik to validate a users input into a form. I seem to be getting an error of invalid hook call.

function UserRegistration  (props)  {
  const Stack = createStackNavigator();
  const { handleSubmit } = props;
  const onSubmit = (values) => console.log(values);
  const renderInput = ({ input: { onChange, ...input }, ...rest}) => {
  return <TextInput style= {CreditRegStyle.input} onChangeText={onChange} {...input} {...rest}/>};
  const {deviceHeight, deviceWidth } = Dimensions.get('window')
  let keyboardVerticalOffset = -50;
  const smallDeviceHeight = 650;

  const formik = useFormik({
    initialValues: {
      full_name: "",
      last_name: "",
      email_address: "",
      Day: "",
      Month: "",
      Year: "",
    }
  });
  return (
    
    <KeyboardAvoidingView
      style={CreditRegStyle.container}
      behavior="padding"
    >
    <View style={CreditRegStyle.Header}>
      <Text style={CreditRegStyle.Header}>
        Basic Details
      </Text>
  
      <Text style={CreditRegStyle.Text}>
      We need some details from you to process your application!
      </Text>
   
      <Field
        style={CreditRegStyle.FirstName}
        type="first_name"
        name="first_name"
        onChange={handleChange}
        onBlur={handleBlur}
        value={values.first_name}
        props={{
          placeholder: 'First Name'
        }}
        component={renderInput}
      />

Then here is the export function.

export default UserRegistration({
  mapPropsToValues: () => ({ first_name: '', last_name: '', email_address: '', Day: '', Month: '', Year: '',  }),
  validationSchema: ProfileSchema,
  handleSubmit: (values, { setSubmitting }) => {
    setTimeout(() => {
      alert(JSON.stringify(values, null, 2));
      setSubmitting(false);
    }, 1000);
  },
  displayName: 'UserRegistration'
})(UserRegistration);

I have tried everything and this error is not very forthcoming so I am unable to work out where the error is and how to solve it.

question from:https://stackoverflow.com/questions/66059304/formik-validation-calling-hooks-outside-a-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...