【发布时间】: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