Skip to content

Instantly share code, notes, and snippets.

@AlaBenAicha
Created October 2, 2022 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlaBenAicha/e71850af55d1ca79e1ba585e6d4e3a49 to your computer and use it in GitHub Desktop.
Save AlaBenAicha/e71850af55d1ca79e1ba585e6d4e3a49 to your computer and use it in GitHub Desktop.
import { useState } from "react"
import { useSelector } from "react-redux"
import { pokemonApi, useGetPokemonByNameQuery } from "../services/pokemon"
import { initializeStore, removeUndefined } from "../store"
export default function Home(props) {
const {data: pokemonList} = useSelector(pokemonApi.endpoints.getPokemonList.select())
const [pokemon, setPokemon] = useState(props.initialPokemon)
const {data: currentPokemon} = useGetPokemonByNameQuery(pokemon)
return (
<>
<h1>Hi</h1>
<h2>You caught {pokemon}! They can have one of these abilities: {currentPokemon.abilities.map(ab => ab.ability.name).join(', ')}</h2>
<p>
Catch another!
<select value={pokemon} onChange={e => setPokemon(e.currentTarget.value)}>
{pokemonList.results.map(pok => <option>{pok.name}</option>)}
</select>
</p>
</>
)
}
export async function getServerSideProps() {
const store = initializeStore()
await store.dispatch(pokemonApi.endpoints.getPokemonList.initiate())
const {data: pokemonList} = pokemonApi.endpoints.getPokemonList.select()(store.getState())
const initialPokemon = pokemonList.results[0].name
await store.dispatch(pokemonApi.endpoints.getPokemonByName.initiate(initialPokemon))
// queryRef.unsubscribe() // I am not sure if something like this is necessary
return { props: { initialReduxState: removeUndefined(store.getState()), initialPokemon } }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment