Answer the question
In order to leave comments, you need to log in
Invalid hook call, how to use the hook correctly in this case?
There is such a component
import PokemonItem from './../PokemonItem';
function PokemonList() {
const [pokemons, setPokemons] = useState({
pokemonList: [],
loading: true,
});
useEffect(()=>{
fetch('https://pokeapi.co/api/v2/pokemon/')
.then(res => res.json())
.then(body => {
setPokemons({
pokemonList: body.results,
loading: false,
})
})
}, [])
let pokemonItems = pokemons.pokemonList.map((pokemon, index) => {
return <PokemonItem name={pokemon.name} key={index} url={pokemon.url} />
})
return(
<div className="pokemon-list">
{pokemonItems}
</div>
)
}
function PokemonItem(props) {
const [pokemon, setPokemon] = useState({
pokemonInfo: [],
loading: true,
});
return(
<li className="pokemon-item">
<div className="pokemon-item__profile">
<img src="" alt="" className="pokemon-item__sprite"/>
<span className="pokemon-item__name">
{props.name}
</span>
</div>
</li>
)
}
Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.
Answer the question
In order to leave comments, you need to log in
and if in PokemonItem we rename the variable in the hook?
maybe react somehow cunningly puts everything into one function in PokemonList, and then it turns out that
pokemons.pokemonList.map((pokemon, index) => {
.....
const [pokemon, setPokemon] = useState({..
// одинаковое имя переменной pokemon
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question