I am trying to learn using TypeScript in my React projects.
I made a type for the incoming data format (name and url).
type PokedexType = {
name: string;
url: string;
}
The data returned from the API is an array of objects (with name and url).
{
"results": [
{
"name": "national",
"url": "https://pokeapi.co/api/v2/pokedex/1/"
},
{
"name": "kanto",
"url": "https://pokeapi.co/api/v2/pokedex/2/"
},
// ...
];
}
In my useEffect(), I am storing these objects in a state with setState().
useEffect(() => {
api.get('/pokedex')
.then((response) => {
setPokedex(response.data.results);
})
.catch((error) => {
console.log(error);
});
});
Because the API returns an array, I am confused about the correct way to setup useState().
// this works
const [pokedex, setPokedex] = useState<Array<PokedexType>>([{ name: "", url: "" }]);
// these also work
const [pokedex, setPokedex] = useState<PokedexType>({ name: "", url: "" });
const [pokedex, setPokedex] = useState<Array<PokedexType>>();
const [pokedex, setPokedex] = useState(); // (with checking)
I understand that specifying type is for type checking, but if the state is overwritten immediately, does this matter?
I am trying to set the states name (i.name) as value for my select, what would be the correct way to do this? Since the state is now an array, how can I set the value to the corresponding property (like this)?
<select value={pokedex.name}>
useState<Array<PokedexType>>([{ name: "", url: "" }]);just beuseState<PokedexType[]>([{ name: "", url: "" }]);?arrname[]andArray<arrname>)type[]and a complex types (defined inline / composite types) should useArray<{ name: string; url: string; }>orArray<typeOne & typeTwo>. I do not know if this is a preference only linter rule or has something to do with type resolution speed.