如何在 Redux Forms 的表单提交之前进行调度? (使用打字稿)
How to make a dispatch before form Submit in Redux Forms ? (using TypeScript)
这是我在单独文件中的提交函数,我想做的是在重定向之前进行分派以将登录状态更改为 true
import React from "react";
import { SubmissionError } from "redux-form";
import { Redirect } from "react-router-dom";
import ax from "axios";
function submit(values: any) {
return ax.get("./UserCredentials.json").then(res => {
console.log(values);
if (!values.username || !values.password) {
throw new SubmissionError({
username: "Please enter your credentials",
_error: "Login failed!"
});
} else if (res.data[0].username !== values.username) {
throw new SubmissionError({
username: "User does not exist",
_error: "Login failed!"
});
} else if (res.data[0].password !== values.password) {
throw new SubmissionError({
password: "Wrong password",
_error: "Login failed!"
});
} else {
//console.log("redirect to profile");
//props.dispatch(login()); // i need to make dispatch here
return <Redirect to="/profile" />;
}
});
}
export default submit;
我想不出正确的方法
像这样使您的函数异步:
import React from "react";
import { SubmissionError } from "redux-form";
import ax from "axios";
async function submit(values) {
const result = await ax.get("./UserCredentials.json").then(res => {
console.log(values);
if (!values.username || !values.password) {
throw new SubmissionError({
username: "Please enter your credentials",
_error: "Login failed!"
});
} else if (res.data[0].username !== values.username) {
throw new SubmissionError({
username: "User does not exist",
_error: "Login failed!"
});
} else if (res.data[0].password !== values.password) {
throw new SubmissionError({
password: "Wrong password",
_error: "Login failed!"
});
}
});
if (result) {
// do your stuff
}
}
export default submit;
这是我在单独文件中的提交函数,我想做的是在重定向之前进行分派以将登录状态更改为 true
import React from "react";
import { SubmissionError } from "redux-form";
import { Redirect } from "react-router-dom";
import ax from "axios";
function submit(values: any) {
return ax.get("./UserCredentials.json").then(res => {
console.log(values);
if (!values.username || !values.password) {
throw new SubmissionError({
username: "Please enter your credentials",
_error: "Login failed!"
});
} else if (res.data[0].username !== values.username) {
throw new SubmissionError({
username: "User does not exist",
_error: "Login failed!"
});
} else if (res.data[0].password !== values.password) {
throw new SubmissionError({
password: "Wrong password",
_error: "Login failed!"
});
} else {
//console.log("redirect to profile");
//props.dispatch(login()); // i need to make dispatch here
return <Redirect to="/profile" />;
}
});
}
export default submit;
我想不出正确的方法
像这样使您的函数异步:
import React from "react";
import { SubmissionError } from "redux-form";
import ax from "axios";
async function submit(values) {
const result = await ax.get("./UserCredentials.json").then(res => {
console.log(values);
if (!values.username || !values.password) {
throw new SubmissionError({
username: "Please enter your credentials",
_error: "Login failed!"
});
} else if (res.data[0].username !== values.username) {
throw new SubmissionError({
username: "User does not exist",
_error: "Login failed!"
});
} else if (res.data[0].password !== values.password) {
throw new SubmissionError({
password: "Wrong password",
_error: "Login failed!"
});
}
});
if (result) {
// do your stuff
}
}
export default submit;