加密貨幣如今風靡一時,隨著可用的硬幣數量過多,有一個工具可以輕鬆搜尋和查看它們的詳細資訊是至關重要的。 Crypto Finder 應用程式就是這樣做的。該應用程式使用 React 構建,為用戶搜尋、過濾和查看加密貨幣詳細資訊提供無縫體驗。
加密貨幣查找器應用程式包括:
以下是專案結構的快速概述:
要開始使用 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中文網其他相關文章!