【问题标题】:TouchOpacity onPress request is not working with formik and react-native-text-input-maskTouchOpacity onPress 请求不适用于 formik 和 react-native-text-input-mask
【发布时间】:2021-01-14 08:13:53
【问题描述】:

当我尝试登录时,按钮未发送请求。在按钮前我应该控制输出。似乎 onChangeText 和 onChange 在 TextInputMask 中无法正常工作。 TouchableOpacity 标签有 onPress 事件,其中 onPress={()=>formikProps.handleSubmit()} 没有触发 formik 的 onSubmit 属性。 这里我使用 yup 进行验证,使用 Formik 提交数据。

    const validationSchema = yup.object().shape({
  phoneNumber: yup
    .string()
    .label("Phone Number")

    .required("Phone Number is required."),
  password: yup.string().label("Password").required("Password is required."),
});
class Home extends Component {
  constructor(props) {
    super(props);

    this.state = {
      username: "",
      password: "",
      press: false,
      ButtonStateHolder: false,
    };
  }
render() {
    return (
      <ImageBackground style={{ width: "100%", height: "100%" }} source={bgImg}>
        <ScrollView>         
          <Formik
            initialValues={{ phoneNumber: "", password: "" }}
            onSubmit={(values, actions) => {
              this.setState({
                password: values.password,
                ButtonStateHolder: true,
              });
              console.warn(this.state.phoneNumber);
              console.warn(this.state.password);

            }}
            validationSchema={validationSchema}
          >
            {(formikProps) => (
              <React.Fragment>
                <View>
                  <View>
                    <Text>Phone number</Text>
                    <TextInputMask
                      keyboardType="number-pad"
                      ref={(ref) => (this.phoneField = ref)}
                      onChangeText={(formatted, extracted) => {                        
                        this.setState({
                          phoneNumber: extracted,
                        });
                      }}
                      onChange={formikProps.handleChange("phoneNumber")}
                      onBlur={formikProps.handleBlur("phoneNumber")}
                      placeholder={""}
                      mask={"([000]) [000] [0000]"}
                    />
                    <Text style={styles.errMsg}>
                      {formikProps.touched.phoneNumber &&
                        formikProps.errors.phoneNumber}
                    </Text>
                  </View>
                  <View>
                    <Text style={styles.formLable}>Password</Text>
                    <TextInput
                      onChangeText={formikProps.handleChange("password")}
                      onBlur={formikProps.handleBlur("password")}
                      placeholder={""}
                      returnKeyType={"done"}
                      autoCapitalize={"none"}
                      autoCorrect={false}
                    />
                    <Text style={styles.errMsg}>
                      {formikProps.touched.password &&
                        formikProps.errors.password}
                    </Text>
                    
                    <TouchableOpacity
                      activeOpacity={0.7}
                      style={styles.btnEye}
                      onPress={this.showPass}
                    >
                      <Image source={eyeImg} style={styles.iconEye} />
                    </TouchableOpacity>
                  </View>
                </View>
                <View style={styles.loginBottom}>
                  <TouchableOpacity
                    style={styles.button}
                    onPress={() => formikProps.handleSubmit()}
                  >
                    <Text style={styles.buttonText}> Login </Text>
                  </TouchableOpacity>
                
                </View>
              </React.Fragment>
            )}
          </Formik>
        </ScrollView>
      </ImageBackground>
    );
  }
}

export default Home;

onPress 控制台日志未打印 有人请帮忙解决这个问题

【问题讨论】:

  • 我没有看到任何 console.log
  • 你在说哪个 onPress ?
  • @HichamELBSI 您可以在 TouchOpacity 标签中看到 onPress 事件,其中 onPress={() => formikprops.handleSubmit()} 和在 formikProps 的 onSubmit 处理程序中我将当前状态设置为新状态。然后我 console.warn() 状态。
  • 感谢@HichamELBSI 的回复。如果您知道解决此问题的方法,请在此处讨论

标签: react-native react-navigation formik yup


【解决方案1】:

Formik 将为您管理输入的状态,因此您无需声明它。

const validationSchema = yup.object().shape({
  phoneNumber: yup
    .string()
    .label("Phone Number")

    .required("Phone Number is required."),
  password: yup.string().label("Password").required("Password is required."),
});

class Home extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <View>
        <ScrollView>         
          <Formik
            initialValues={{ phoneNumber: "", password: "" }}
            onSubmit={(values, actions) => {
              alert(JSON.stringify(values));
            }}
            validationSchema={validationSchema}
          >
            {({handleChange, values, handleSubmit, touched, errors, handleBlur}) => (
              <React.Fragment>
                <View>
                  <View>
                    <Text>Phone number</Text>
                    <TextInput
                      keyboardType="number-pad"
                      onChangeText={handleChange}
                      onChange={handleChange("phoneNumber")}
                      onBlur={handleBlur("phoneNumber")}
                      placeholder={""}
                      mask={"([000]) [000] [0000]"}
                      value={values.phoneNumber}
                    />
                    <Text>
                      {touched.phoneNumber &&
                        errors.phoneNumber}
                    </Text>
                  </View>
                  <View>
                    <Text>Password</Text>
                    <TextInput
                      onChangeText={handleChange("password")}
                      onBlur={handleBlur("password")}
                      placeholder={""}
                      returnKeyType={"done"}
                      autoCapitalize={"none"}
                      autoCorrect={false}
                      value={values.password}
                    />
                    <Text>
                      {touched.password &&
                        errors.password}
                    </Text>
                  </View>
                </View>
                <View >
                  <TouchableOpacity
                    onPress={() => handleSubmit()}
                  >
                    <Text> Login </Text>
                  </TouchableOpacity>
                
                </View>
              </React.Fragment>
            )}
          </Formik>
        </ScrollView>
      </View>
    );
  }
}

您还可以使用useFormik 挂钩而不是Formik 组件来管理表单

import { useFormik } from 'formik';

const validationSchema = yup.object().shape({
  phoneNumber: yup
    .string()
    .label("Phone Number")

    .required("Phone Number is required."),
  password: yup.string().label("Password").required("Password is required."),
});

const Home = () => {
    const { handleChange, values, handleSubmit, touched, errors, handleBlur } = useFormik({
      initialValues: {
         phoneNumber: "", password: ""
      },
      onSubmit: (values, actions) => {
         alert(JSON.stringify(values));
      },
      validationSchema
    });

    console.log(values);
    
    return (
      <View>
        <ScrollView>
                <View>
                  <View>
                    <Text>Phone number</Text>
                    <TextInput
                      keyboardType="number-pad"
                      onChangeText={handleChange}
                      onChange={handleChange("phoneNumber")}
                      onBlur={handleBlur("phoneNumber")}
                      placeholder={""}
                      mask={"([000]) [000] [0000]"}
                      value={values.phoneNumber}
                    />
                    <Text>
                      {touched.phoneNumber &&
                        errors.phoneNumber}
                    </Text>
                  </View>
                  <View>
                    <Text>Password</Text>
                    <TextInput
                      onChangeText={handleChange("password")}
                      onBlur={handleBlur("password")}
                      placeholder={""}
                      returnKeyType={"done"}
                      autoCapitalize={"none"}
                      autoCorrect={false}
                      value={values.password}
                    />
                    <Text>
                      {touched.password &&
                        errors.password}
                    </Text>
                  </View>
                </View>
                <View >
                  <TouchableOpacity
                    onPress={() => handleSubmit()}
                  >
                    <Text> Login </Text>
                  </TouchableOpacity>

                </View>
        </ScrollView>
      </View>
    );
  }
}

如果我误解了什么或者对你有帮助,请告诉我

【讨论】:

  • 感谢您的回复。我需要在我的组件中使用这些数据。那么,如果我不在组件内部声明状态,formik 管理组件内部的数据也会像我们使用状态管理组件内部的数据一样?
  • 您可以将 useFormik 与功能组件一起使用。查看我编辑的答案。
猜你喜欢
  • 2023-03-21
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 2016-05-12
  • 2019-06-14
  • 1970-01-01
相关资源
最近更新 更多