加密货币如今风靡一时,随着可用硬币的数量过多,有一个工具可以轻松搜索和查看它们的详细信息是至关重要的。 Crypto Finder 应用程序就是这样做的。该应用程序使用 React 构建,为用户搜索、过滤和查看加密货币详细信息提供无缝体验。
Crypto Finder 应用程序包括:
以下是项目结构的快速概述:
要开始使用 Crypto Finder 应用程序,请按照以下步骤操作:
git clone https://github.com/abhishekgurjar-in/Crypto-Finder cd crypto-finder
npm install
npm start
App.js 组件设置路由并包含导航栏和页脚组件。
import React from "react"; import CryptoFinder from "./components/CryptoFinder"; import "./App.css"; import Navbar from "./components/Navbar"; import Footer from "./components/Footer"; import {Route,Routes} from "react-router-dom" import CryptoDetails from "./components/CryptoDetails"; const App = () => { return ( <div> <Navbar /> <Routes> <Route path="/" element={<CryptoFinder/>}/> <Route path="/details/:id" element={<CryptoDetails/>}/> </Routes> <Footer /> </div> ); }; export default App;
CryptoFinder.js 组件处理获取和显示加密货币列表。它包括一个用于过滤结果的搜索栏。
import React, { useEffect, useState } from "react"; import axios from "axios"; import { Link } from "react-router-dom"; const CryptoFinder = () => { const [search, setSearch] = useState(""); const [crypto, setCrypto] = useState([]); const [filteredCrypto, setFilteredCrypto] = useState([]); useEffect(() => { axios .get(`https://api.coingecko.com/api/v3/coins/markets`, { params: { vs_currency: "inr", order: "market_cap_desc", per_page: 100, page: 1, sparkline: false, }, }) .then((res) => { setCrypto(res.data); setFilteredCrypto(res.data); }); }, []); const handleSearch = () => { const filteredData = crypto.filter((data) => { return data.name.toLowerCase().includes(search.toLowerCase()); }); setFilteredCrypto(filteredData); }; if (crypto.length === 0) { return ( <div className="loader-box"> <div className="loader"></div> </div> ); } return ( <div className="crypto-finder"> <div className="input-box"> <input type="text" value={search} onChange={(e) => setSearch(e.target.value)} onKeyDown={handleSearch} placeholder="Search for a cryptocurrency" /> <button onClick={handleSearch}>Search</button> </div> <div className="cards"> {filteredCrypto.map((val, id) => ( <div className="card" key={id}> <img src={val.image} alt={val.name} /> <h1>{val.name}</h1> <h4>{val.symbol.toUpperCase()}</h4> <h4>₹{val.current_price.toFixed(2)}</h4> <Link to={`/details/${val.id}`}> <button>View Details</button> </Link> </div> ))} </div> </div> ); }; export default CryptoFinder;
CryptoDetails.js 组件获取并显示有关所选加密货币的详细信息。
import React, { useEffect, useState } from "react"; import axios from "axios"; import { useParams } from "react-router-dom"; const CryptoDetails = () => { const { id } = useParams(); const [cryptoDetails, setCryptoDetails] = useState(null); useEffect(() => { axios .get(`https://api.coingecko.com/api/v3/coins/${id}`, { params: { localization: false, }, }) .then((res) => { setCryptoDetails(res.data); }); }, [id]); if (!cryptoDetails) { return ( <div className="loader-box"> <div className="loader"></div> </div> ); } return ( <div className="crypto-details"> <div className="basic-details-image-box"> <div className="basic-details"> <h1>{cryptoDetails.name}</h1> <h4>{cryptoDetails.symbol.toUpperCase()}</h4> <h4> Current Price: ₹ {cryptoDetails.market_data.current_price.inr.toFixed(2)} </h4> </div> <div className="image-box"> <img src={cryptoDetails.image.large} alt={cryptoDetails.name} /> </div> </div> <div className="detail-desc"> <h3>Description :</h3> <p >{cryptoDetails.description.en}</p> </div> <div className="market-and-additional"> <div className="market-data"> <h2>Market Data</h2> <p> <b>Market Cap: </b>₹ {cryptoDetails.market_data.market_cap.inr.toLocaleString()} </p> <p> <b>Total Volume: </b>₹ {cryptoDetails.market_data.total_volume.inr.toLocaleString()} </p> <p><b>24h High:</b> ₹{cryptoDetails.market_data.high_24h.inr}</p> <p><b>24h Low:</b> ₹{cryptoDetails.market_data.low_24h.inr}</p> <p> <b> Price Change (24h):</b> ₹ {cryptoDetails.market_data.price_change_24h.toFixed(2)} </p> <p> <b>Price Change Percentage (24h):</b>{" "} {cryptoDetails.market_data.price_change_percentage_24h.toFixed(2)}% </p> </div> <div className="additional-info"> <h2>Additional Information</h2> <p><b>Genesis Date:</b> {cryptoDetails.genesis_date || "N/A"}</p> <p> <b>Homepage:</b>{" "} <a href={cryptoDetails.links.homepage[0]} target="_blank" rel="noopener noreferrer" > {cryptoDetails.links.homepage[0]} </a> </p> <p> <b> Blockchain Site:</b>{" "} <a href={cryptoDetails.links.blockchain_site[0]} target="_blank" rel="noopener noreferrer" > {cryptoDetails.links.blockchain_site[0]} </a> </p> </div> </div> </div> ); }; export default CryptoDetails;
Navbar.js 组件为应用程序提供标题。
import React from 'react' const Navbar = () => { return ( <div className="navbar"> <h1>Crypto Finder</h1> </div> ) } export default Navbar
Footer.js 组件为应用程序提供页脚。
import React from 'react' const Footer = () => { return ( <div className="footer"> <p>Made with ❤️ by Abhishek Gurjar</p> </div> ) } export default Footer
您可以在此处查看 Crypto Finder 应用程序的实时演示。
构建 Crypto Finder 应用程序是一次有趣且具有教育意义的体验。它演示了如何使用 React 来获取和显示数据、处理路由以及创建响应式且用户友好的界面。我希望这个项目对您有所帮助,并激励您使用 React 创建自己的应用程序!
阿布舍克·古贾尔
您可以根据您的喜好或您可能已实现的其他功能随意调整或添加更多详细信息。
以上是使用 React 构建加密货币查找器应用程序的详细内容。更多信息请关注PHP中文网其他相关文章!