0

I made a request with AXIOS to the server and then the server returned the value as follows:

enter image description here

I want to save it in state, here I use react hooks, when I want to set state with data as shown I get error:

Argument of type '() => IterableIterator<IMahasiswa>' is not assignable to parameter of type 'SetStateAction<undefined>'.
  Type 'IterableIterator<IMahasiswa>' is not assignable to type 'undefined'.ts(2345)

and this is my code:

enter image description here

and my interface:

enter image description here

how do I set the interface for setMahasiswa I have tried <IMahasiswa[]> but it gives the same error, except that in the res.values ​​section it is replaced with res it is successfully returned, but it will throw another error when I using map for looping

2
  • 2
    Can you post the code instead of screenshots? Commented Jun 27, 2021 at 2:00
  • Did you actually get the exact same error when using <IMahasiswa[]>? Can you include that error message? Commented Jun 27, 2021 at 2:08

1 Answer 1

1

My assumption is that your axios.get(url[, config]) returns an any type by default. So, your data there also has any type unless you cast it to IMahasiswa[].

However, my suggested solution is to define the type at axios.get, here's the outline from their typedef.

export interface AxiosInstance {
  //...
  get<T = any, R = AxiosResponse<T>>(url: string, config?: AxiosRequestConfig): Promise<R>;
  //...
}

Therefore, you can do something like this;

await axiosClient.get<IMahasiswa[]>("asd/asd", {});

Here's the full code example;

interface IMahasiswa {
  name: string;
  age: number;
}

const useTest = () => ({
  async fetch(): Promise<IMahasiswa[]> {
    const { data } = await axiosClient.get<IMahasiswa[]>("asd/asd", {});
    return data; // data here has the type of IMahasiswa[]
  },
});

const axiosClient = axios.create({
  baseURL: "http://localhost:1231",
});

const TestComp = () => {
  const [mahasiswa, setMahasiswa] = React.useState<IMahasiswa[]>([]);
  const testFn = useTest();

  useEffect(() => {
    testFn.fetch().then((res) => {
      setMahasiswa(res);
    });
  }, []);

  return <div>asd</div>;
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.