React-Native + Mobx项目实战--探花交友APP--登录页--输入框
import React from 'react';
import { View, TextInput } from 'react-native';
import { observer } from 'mobx-react';
import { Form, Field } from 'mobx-react-form';
import { InputField, Button } from '../../components';
import { loginValidation } from '../../validations';
import { login } from '../../api';
import { globalStyles } from '../../styles';
@observer
class LoginScreen extends React.Component {
form = new Form(loginValidation, {
onSubmit: async (values) => {
const response = await login(values);
if (response.ok) {
// 登录成功的处理逻辑
} else {
// 登录失败的处理逻辑
}
}
});
render() {
return (
<View style={globalStyles.container}>
<Form form={this.form}>
<Field name="email">
{({ field, form }) => (
<InputField
{...field}
label="Email"
autoCapitalize="none"
keyboardType="email-address"
returnKeyType="next"
onSubmitEditing={() => form.getFieldRef('password').focus()}
/>
)}
</Field>
<Field name="password">
{({ field, form }) => (
<InputField
{...field}
label="Password"
secureTextEntry
returnKeyType="done"
onSubmitEditing={form.submit}
/>
)}
</Field>
<Button title="Login" onPress={this.form.submit} />
</Form>
</View>
);
}
}
export default LoginScreen;
这个代码实例展示了如何在React Native项目中使用Mobx-React Form来创建登录表单,并处理用户输入的验证和提交。同时,展示了如何使用自定义的InputField
组件和Button
组件来提升用户界面的一致性和可复用性。
评论已关闭