简体   繁体   English

React Native - 注册屏幕不起作用

[英]React Native - Register Screen not working

I can't submit a form.我无法提交表格。 My states:我的状态:

function RegisterScreen({ navigation }) {
  const [formData, setFormData] = React.useState({
    name: "",
    surname: "",
    email: "",
    password: "",
    password2: "",
  });

onSubmit:提交:

const onSubmit = async (e) => {
    const { name, email, password, password2 } = formData;
    console.log(formData);
  };

Return here: Inputs.在此处返回:输入。 I did onChangeText and value here.我在这里做了 onChangeText 和 value。

return (
  <View>
      <TextInput
        placeholder={i18n.t("name")}
        value={formData.name}
        onChangeText={(name) => setFormData(name)}
        id="name"
        required
      />
    <TextInput
      placeholder={i18n.t("email")}
      value={formData.email}
      onChangeText={(email) => setFormData(email)}
      id="email"
      required
    />
    <TextInput
      secureTextEntry={true}
      placeholder={i18n.t("password")}
      value={formData.password}
      onChangeText={(password) => setFormData(password)}
      id="password"
    />

    <Button
      title={i18n.t("register")}
      onPress={() => onSubmit()}
    />
  </View>
);   }

It just returns password.它只是返回密码。 I am expecting all form data.我期待所有表单数据。 I think the code has an error.我认为代码有错误。

it's because this is overwriting everything at the end:这是因为这最终覆盖了所有内容:

onChangeText={(password) => setFormData(password)}

you need to do你需要做

onChangeText={(name) => setFormData({...formData, name})}
onChangeText={(email) => setFormData({...formData, email})}
onChangeText={(password) => setFormData({...formData, password})}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM