React native 我如何在Formik表单上获得此handleSubmit以执行Post请求
Formik表单上的handleSubmit函数未正确地向后端API发送Post请求。我相信我已经掌握了97%的代码,但我不太理解其中的细微差别 表单有一个email字段,我需要Submit将该email字符串传递给POST请求。下面的代码有4个文件:React native 我如何在Formik表单上获得此handleSubmit以执行Post请求,react-native,react-navigation,mobx,formik,React Native,React Navigation,Mobx,Formik,Formik表单上的handleSubmit函数未正确地向后端API发送Post请求。我相信我已经掌握了97%的代码,但我不太理解其中的细微差别 表单有一个email字段,我需要Submit将该email字符串传递给POST请求。下面的代码有4个文件:Enter Email.js,SessionStore.js,UserService.js和request.js 我相信大部分问题都出在Enter Email.js中的handleSubmit函数和SessionStore.js中的@action
Enter Email.js
,SessionStore.js
,UserService.js
和request.js
我相信大部分问题都出在Enter Email.js
中的handleSubmit
函数和SessionStore.js
中的@action sendResetCode
中
谢谢你的帮助
输入-Email.js:
import React, { Component, Fragment } from "react";
import { observer } from "mobx-react/native";
import { Formik } from "formik";
import * as yup from "yup";
@observer
class EnterEmail extends Component {
handleSubmit = async ({ <----- function I think has the problem
email,
values,
}) => {
const {
navigation: { navigate },
sessionStore: { sendResetCode }
} = this.props;
let response = await sendResetCode({ <------ the function to make the POST request!
email: email
});
if (response) {
return navigate("VerificationCode", { values });
} else {
return Alert(
{ message: "Error sending the reset link" },
"There was an error sending the reset link. Please try again."
);
}
}
render() {
return (
<SafeAreaView style={{ flex: 1 }}>
<Text>Enter your e-mail to receive a link to reset your password</Text>
<Formik
initialValues={{ email: ''}}
onSubmit={this.handleSubmit}
validationSchema={yup.object().shape({
email: yup
.string()
.email()
.required(),
})}
>
{({ values, handleChange, setFieldTouched, handleSubmit }) => (
<Fragment>
<View>
<View>
<TextInputGroup
id="email"
keyboardType="email-address"
label="Email"
onBlur={() => setFieldTouched("email")}
value={values.email}
onChangeText={handleChange("email")}
/>
</View>
</View>
<View>
<LargeButton
text="Send Reset Link"
onPress={handleSubmit}
/>
</View>
</Fragment>
)}
</Formik>
</SafeAreaView>
);
}
}
export default EnterEmail;
如果有必要,以下是Package.json中的版本:
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
"native-base": "^2.12.1",
"react": "16.8.0",
"react-navigation": "^3.11.0",
"formik": "^1.5.8",
"mobx": "5.9.4",
"mobx-react": "5.4.4",
在您的案例中,POST请求的发送方式到底有哪些不恰当之处?在您的案例中,POST请求的发送方式到底有哪些不恰当之处?
// to send code to email that was entered:
userService.sendResetCode = async (
body: Object
): Promise<ApiResponse<Nurse>> => {
const response: Response = await request.post(
`/v1/password_resets`, <---- backend endpoint which works
body
);
return handleResponse(response);
};
post(url, body, useBody) {
return fetch(this._buildUrl(url), {
method: "POST",
headers: getCommonHeaders(useBody),
body: useBody ? body : JSON.stringify(body)
});
},
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
"native-base": "^2.12.1",
"react": "16.8.0",
"react-navigation": "^3.11.0",
"formik": "^1.5.8",
"mobx": "5.9.4",
"mobx-react": "5.4.4",