📅  最后修改于: 2023-12-03 14:47:00.066000             🧑  作者: Mango
本应用程序使用 React.js 构建,旨在提供一个交互式的神奇宝贝图鉴。
本应用程序使用 PokeAPI 提供的神奇宝贝数据。
import React, { useState, useEffect } from 'react';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import axios from 'axios';
import PokemonList from './components/PokemonList';
import PokemonDetail from './components/PokemonDetail';
import SearchBar from './components/SearchBar';
import RandomBtn from './components/RandomBtn';
function App() {
const [pokemonData, setPokemonData] = useState([]);
const [searchTerm, setSearchTerm] = useState('');
useEffect(() => {
const fetchPokemonData = async () => {
try {
const response = await axios.get('https://pokeapi.co/api/v2/pokemon?limit=150');
setPokemonData(response.data.results);
} catch (error) {
console.error(error);
}
};
fetchPokemonData();
}, []);
const filteredPokemonData = pokemonData.filter((item) => item.name.includes(searchTerm));
return (
<Router>
<div className="container mt-5">
<h1 className="text-center mb-4">React.js 神奇宝贝应用程序</h1>
<SearchBar searchTerm={searchTerm} setSearchTerm={setSearchTerm} />
<RandomBtn />
<Switch>
<Route exact path="/">
<PokemonList pokemonData={filteredPokemonData} />
</Route>
<Route path="/pokemon/:id">
<PokemonDetail />
</Route>
</Switch>
</div>
</Router>
);
}
export default App;