I am learning TypeScript and I am stuck on the correct assignment for my setState
that I pass to a component.
/index.tsx
import { Dispatch, SetStateAction, useState } from "react";
import FetchPokeAPI from "./api/FetchPokeAPI";
import Header from "./components/Header";
import ItemList from "./components/ItemList";
export interface IAllItems {
name: string;
url: string;
} [];
export default function Home() {
const [allItems, setAllItems] = useState<IAllItems[]>([]);
FetchPokeAPI(setAllItems);
return (
<>
<Header />
<ItemList items={allItems} />
</>
)
}
/FetchPokeAPI.tsx
import { Dispatch, SetStateAction, useEffect } from "react";
import { IAllItems } from "./../index";
interface IProps {
setAllItems: Dispatch<SetStateAction<IAllItems[]>>;
allItems: IAllItems[];
}
export default function FetchPokeAPI({setAllItems}:IProps) {
const pokeapiURL:string = "https://pokeapi.co/api/v2/item/";
useEffect(() => {
fetch(pokeapiURL)
.then((response) => response.json())
.then((data) => {
setAllItems(data.results);
})
.catch((error) => console.error(error));
}, [pokeapiURL]);
}
I get this message:
Argument of type 'Dispatch<SetStateAction<IAllItems[]>>'
is not assignable to parameter of type 'IProps'
Thanks in advance :)
Other forum posts have helped me a bit, but now I can't get any further.