Home > Back-end >  React-Typescript: Expected an assignment or function call and instead saw an expression
React-Typescript: Expected an assignment or function call and instead saw an expression

Time:12-09

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:

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.

auth.ts: (Action Creator)

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.

types.ts:

export enum Types {
  SIGNUP = "SIGNUP"
}

Through this file I was able to communicate with the backend.

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;

CodePudding user response:

You need to just return this from 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>) => {

CodePudding user response:

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".

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.

  • Related