Here is the app.svelte code,
I have a search bar there where you can search the Pokémon by the name of it, but it does not show the right photo of the searched Pokémon :
<script>
import { onMount } from 'svelte';
import PokemonCard from './PokemonCard.svelte';
import { Circle3 } from 'svelte-loading-spinners';
let pokemons = [];
onMount(async () => {
await new Promise((resolve) => setTimeout(resolve, 3000));
const response = await fetch('https://pokeapi.co/api/v2/pokemon?limit=151');
if (!response.ok) {
throw new Error('Failed to fetch pokemons');
}
const data = await response.json();
pokemons = data.results;
});
let searchString = '';
$: selectedMonsters = pokemons.filter((pokemon) => {
return pokemon.name
.toLowerCase()
.includes(searchString.toLocaleLowerCase());
});
</script>
<main>
<header>
<input
class="search"
type="text"
bind:value={searchString}
placeholder="Pokemon Name"
/>
</header>
<div class="container">
<ul class="grid">
{#each selectedMonsters as { name }, index (index)}
<PokemonCard {name} {index} />
{:else}
<div class="center">
<h1>First generation Pokémons are loading...</h1>
<Circle3 />
</div>
{/each}
</ul>
</div>
</main>
And here is the PokemonCard.svelte :
<script>
export let name;
export let index;
const imgUrl = `https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${
index + 1
}.png`;
</script>
<div class="Card">
<h2>{name}</h2>
<div>
<img src={imgUrl} alt="pokeImg" style="width: 150px; height: 150px;" />
</div>
</div>
I have tried many things but they are not working I think I know why it is not working but I don't how to fix it.