Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If useSWR is dependent on state, the return values are undefined #71

Closed
mustafa-hanif opened this issue Nov 4, 2019 · 2 comments
Closed

Comments

@mustafa-hanif
Copy link

const Pokemon = () => {
  const [pokemon, setPokemon] = useState(null);

  const loadPokemon = (url) => setPokemon(url);

  const { data, error } = useSWR('https://pokeapi.co/api/v2/pokemon', fetch);
  const { pokemonData, pokemonError } = useSWR(pokemon, fetch);
  
  // This is null
  console.log(pokemonData, pokemonError)
  
  if (error) return <div>failed to load</div>
  if (!data) return <div>loading...</div>

  return <div>{data.results.map(pokemon => {
    return <h2 key={pokemon.name} onClick={() => loadPokemon(pokemon.url)}>{pokemon.name}</h2>
  })}

  </div>
}

Don't know what I am doing wrong. But the second useSWR is always undefined, even though I see the network calls going through.

@mrceperka
Copy link

mrceperka commented Nov 4, 2019

Hello, please try this

const { data: pokemonData, error: pokemonError} = ...

@mustafa-hanif
Copy link
Author

That works thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants