I'm setting a value when you click on Back to List. As per logic it should not go back to the 1st page which appears when I load the browser. I specified a value
<a href="#" onClick={() => setPokemon(pokemon.name)}>
If I wanted to go back to the list page I should have passed null like this.
<a href="#" onClick={() => setPokemon(null)}>
Here is the full code.
import "./styles.css";
import { QueryClient, QueryClientProvider, useQuery } from "react-query";
import axios from "axios";
import { useState } from "react";
const queryClient = new QueryClient();
function App() {
// useEffect(() => {
// queryClient.clear();
// }, []);
const [pokemon, setPokemon] = useState(null);
return (
<QueryClientProvider client={queryClient}>
<div className="App">
{pokemon ? (
<Pokemon pokemon={pokemon} setPokemon={setPokemon} />
) : (
<PokemonList setPokemon={setPokemon} />
)}
</div>
</QueryClientProvider>
);
}
export default App;
function usePokemonList() {
return useQuery("pokemon", async () => {
const { data } = await axios.get(
"https://pokeapi.co/api/v2/pokemon?offset=0&limit=50"
);
return data.results;
});
}
function usePokemon(name) {
return useQuery(["pokemon", name], async () => {
const { data } = await axios.get(
`https://pokeapi.co/api/v2/pokemon/${name}`
);
return data;
});
}
function Pokemon({ pokemon, setPokemon }) {
const { isLoading, data } = usePokemon(pokemon);
return (
<div>
<a href="#" onClick={() => setPokemon(pokemon.name)}>
Back to the list
</a>
{isLoading ? (
<p>loading...</p>
) : (
<div>
<h1>{pokemon}</h1>
<img src={data.sprites.front_default} alt={pokemon} />
</div>
)}
</div>
);
}
function PokemonList({ setPokemon }) {
const { isLoading, data } = usePokemonList();
return (
<div>
{isLoading ? (
<p>loading...</p>
) : (
<ul>
{data.map((pokemon) => (
<li key={pokemon.name}>
<a onClick={() => setPokemon(pokemon.name)} href="#">
{pokemon.name}
</a>
</li>
))}
</ul>
)}
</div>
);
}