[英]React-Typescript: Expected an assignment or function call and instead saw an expression
I'm doing a signup using React and Redux, and I've done all the action and reduser i need, but I got this error:我正在使用 React 和 Redux 进行注册,并且我已经完成了我需要的所有操作和 reduser,但是我收到了这个错误:
Expected an assignment or function call and instead saw an expression
This file is an Action Creator, and through it I used Dispatch to communicate with the Reducer.这个文件是一个 Action Creator,通过它我使用 Dispatch 与 Reducer 进行通信。
auth.ts: (Action Creator) auth.ts:(动作创建者)
export const signupActionCreator = (
email: string,
name: string
) => {
return async (dispatch: Dispatch<UserTypes>) => {
return AuthService.signup(email, name)
.then((res) => {
// tttt
localStorage.setItem('token', res.data)
dispatch({
type: Types.SIGNUP,
// payload: res.data.message,
// tttt
payload: res.data
});
return Promise.resolve();
})
.catch((err) => {
console.log(err);
return Promise.reject();
});
};
};
This is the file and I used a type for each action and I used Enum.这是文件,我为每个操作使用了一个类型,我使用了 Enum。
types.ts:类型.ts:
export enum Types {
SIGNUP = "SIGNUP"
}
Through this file I was able to communicate with the backend.通过这个文件,我能够与后端进行通信。
authServices.ts: authServices.ts:
import API_URL from "../http-common";
import axios from "axios";
const signup = (email: string, name: string) => {
return axios.post(API_URL + "/authentication/sign-up", { email, name });
};
const AuthService = {
signup,
};
export default AuthService;
Through this file I can define the interfaces.通过这个文件,我可以定义接口。
auth.d.ts:身份验证文件:
export interface UserData {
name: string;
email: string;
}
export interface UserState {
data: UserData[];
}
export interface UserAction {
type: string;
payload: any;
}
export type UserTypes = UserAction;
You need to just return this from signupActionCreator
:你只需要从
signupActionCreator
返回这个:
export const signupActionCreator = (email: string, name: string) => {
return async (dispatch: Dispatch<UserAction>) => {
or assign it to the variable:或将其分配给变量:
export const signupActionCreator = (email: string, name: string): void => {
const testVariable = async (dispatch: Dispatch<UserAction>) => {
I read this post and understood what caused the problem:我阅读了这篇文章并了解了导致问题的原因:
https://www.codecademy.com/forum_questions/505a2f774f0e7400020021ba
I traced my code and found that the problem is in the authServices,ts file, I have to assign the variable "response" and then return the variable "response".我跟踪了我的代码,发现问题出在 authServices,ts 文件中,我必须分配变量“response”,然后返回变量“response”。
async function signup(email: string, name: string) {
const response = await axios.post(API_URL + "/authentication/sign-up", { email, name });
return response;
};
Then my problem was solved.然后我的问题就解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.