在React Native中插入具有TextInput值的数据,无法将默认值发送到API

发布于 2025-02-09 08:29:28 字数 894 浏览 1 评论 0原文

我有一个具有textInput's的屏幕。我正在尝试通过API将这些textInput的值插入数据库。当提交而无需输入textInput中时,我将使用textInput中显示的值数据,但是在我的下面代码时,我会得到five>字段是必需 <代码>状态:422 。

const pressHandle = ({ name, userid }) => {
  axios
    .post(userPostLink, {
      name,
      userid,
    })
    .then((response) => {
      console.log(response.data);
    })
    .catch((error) => {
      console.log(error.response);
    });
};
<TextInput onChangeText={(text) => setUsername(text)} placeholder="username" value="steve" />
<TextInput onChangeText={(text) => setEventid(text)} placeholder="userid" value="fle91739" />

<TouchableOpacity onPress={() => pressHandle({ name, userid })}>
  <Text style={{ color: "blue" }}>Submit</Text>
</TouchableOpacity>

I have a screen that has TextInput's. I am trying to insert these TextInput's values into the database through the API. And when submitted without typing in the TextInput, I would use the value data that is showing in the TextInput, but with my my below code I keep getting field is required status: 422.

const pressHandle = ({ name, userid }) => {
  axios
    .post(userPostLink, {
      name,
      userid,
    })
    .then((response) => {
      console.log(response.data);
    })
    .catch((error) => {
      console.log(error.response);
    });
};
<TextInput onChangeText={(text) => setUsername(text)} placeholder="username" value="steve" />
<TextInput onChangeText={(text) => setEventid(text)} placeholder="userid" value="fle91739" />

<TouchableOpacity onPress={() => pressHandle({ name, userid })}>
  <Text style={{ color: "blue" }}>Submit</Text>
</TouchableOpacity>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风情万种。 2025-02-16 08:29:28

首先,您想更改JSX,使用DefaultValue来设置这些默认值。

<TextInput
   onChangeText={text => setUsername(text)}
   placeholder="username"
   value={name}
   defaultValue="steve"
 />
<TextInput
    onChangeText={text => setEventid(text)}
    placeholder="userid"
    value={userid}
    defaultValue="fle91739"
/>

并将您的PressHandle更改为:

const pressHandle = ({ name, userid }) => {
  axios
    .post(userPostLink, {
      name: name || "steve",
      userid: userid || "fle91739",
    })
    .then((response) => {
      console.log(response.data);
    })
    .catch((error) => {
      console.log(error.response);
    });
};

First you would wanna change a little bit your JSX, make use of defaultValue for setting those defaults values.

<TextInput
   onChangeText={text => setUsername(text)}
   placeholder="username"
   value={name}
   defaultValue="steve"
 />
<TextInput
    onChangeText={text => setEventid(text)}
    placeholder="userid"
    value={userid}
    defaultValue="fle91739"
/>

And change your pressHandle, to this:

const pressHandle = ({ name, userid }) => {
  axios
    .post(userPostLink, {
      name: name || "steve",
      userid: userid || "fle91739",
    })
    .then((response) => {
      console.log(response.data);
    })
    .catch((error) => {
      console.log(error.response);
    });
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文