import { QueryClient, QueryClientProvider, keepPreviousData, useQuery, } from '@tanstack/solid-query' import { SolidQueryDevtools } from '@tanstack/solid-query-devtools' import { For, Show, Suspense, createContext, createSignal, useContext, } from 'solid-js' import { isServer } from 'solid-js/web' import { getSearchParams, properCase } from '../utils' import { Link } from './Link' const PokemonIdContext = createContext<() => string>() const usePokemonID = () => { const id = useContext(PokemonIdContext) if (!id) throw new Error('PokemonIdContext not found') return id } const MAX_POKEMONS = 100 export const SolidApp = (props: { pokemon?: string }) => { const client = new QueryClient() const search = getSearchParams(props.pokemon || '') return ( ) } const App = () => { return (
) } const PokemonDetails = () => { const id = usePokemonID() return (
Select a pokemon to see its stats
) } const PokemonDex = (props: { id: string }) => { const pokemon = useQuery(() => ({ queryKey: ['pokemon', props.id], queryFn: async () => { const res = await fetch( `https://pokeapi.co/api/v2/pokemon/${props.id}`, ).then((res) => res.json()) return res }, placeholderData: keepPreviousData, })) const pokemon_stats = useQuery(() => ({ queryKey: ['pokemon', props.id], queryFn: async () => { const res = await fetch( `https://pokeapi.co/api/v2/pokemon/${props.id}`, ).then((res) => res.json()) return res }, select(data) { const nameMap = { hp: 'HP', attack: 'Attack', defense: 'Defense', 'special-attack': 'Special Attack', 'special-defense': 'Special Defense', speed: 'Speed', } const stats = data.stats.map((stat: any) => ({ name: nameMap[stat.stat.name as keyof typeof nameMap], value: stat.base_stat, })) return stats as { name: string; value: number }[] }, placeholderData: keepPreviousData, reconcile: 'name', })) const is_server_rendered = useQuery(() => ({ queryKey: ['is_server_rendered', props.id], queryFn: () => { if (isServer) return true return false }, placeholderData: keepPreviousData, })) return (
{properCase(pokemon.data.name)}
{properCase(pokemon.data?.name {properCase(pokemon.data?.name
This query was rendered on the{' '} client. Reload this page to see the page rendered on the server. } > server. Click on another pokemon to see queries run and render on the client.
{(stat) => (
{stat.name}
{stat.value}
)}
) } const SideNav = () => { const id = usePokemonID() const pokemonsList = useQuery(() => ({ queryKey: ['pokemons'], queryFn: async () => { const res = await fetch( `https://pokeapi.co/api/v2/pokemon?limit=${MAX_POKEMONS}`, ).then((res) => res.json()) return res as { results: { name: string; url: string }[] } }, select(data) { return data.results.map((p) => { const regex = /\/pokemon\/(\d+)\/$/ const match = p.url.match(regex) const id = match ? match[1] : '' return { name: properCase(p.name), id, avatar: `https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/${id}.png`, } }) }, placeholderData: keepPreviousData, reconcile: 'id', })) const activeClass = (pokemonID: string) => id() === pokemonID ? 'bg-gray-50' : 'hover:bg-gray-50' return (
Pokemons
{(pokemon) => ( {pokemon.name} {pokemon.name} )}
) }