我刚开始学习react,我试图用pokemon api构建一个react应用,并收到以下错误:TypeError: pokemon.map is not a function,我不确定.map为什么不是js中的函数,它有什么用吗?与我的扩展程序或下面的代码有关?请帮助,提前谢谢!

App.js

import React, { useState } from 'react';
import PokemonList from './PokemonList'


function App() {
  // eslint-disable-next-line
  const [pokemon, setPokemon] = useState("pokemon-1", "pokemon-2")
  return (
    <PokemonList pokemon={pokemon}/>
  );
}

export default App;



index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));




PokemonList.js

import React from 'react';

export default function PokemonList ({pokemon}) {
  return (
    <div>
    {pokemon.map(p =>(
      <div key={p}>{p}</div>
    ))}
    </div>
  );
}


Pagination.js

import React from 'react';

export default function Pagination() {
  return (
    <>

    </>
  );
}

最佳答案

您可能打算将pokemon的默认值设置为数组
const [pokemon, setPokemon] = useState(["pokemon-1", "pokemon-2"])

注意两个字符串值["pokemon-1", "pokemon-2"]周围的方括号

关于javascript - TypeError:pokemon.map不是函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60026377/

10-10 00:50