使用 React 构建歌词查找器应用程序
介绍
在本教程中,我们将使用 React 创建一个 Lyrics Finder Web 应用程序。该项目非常适合那些想要练习集成 API、管理状态和显示动态内容的人。
项目概况
歌词查找器允许用户通过输入歌曲标题和艺术家姓名来搜索歌词。它从公共 API 获取歌词并将其显示在屏幕上。用户可以快速找到并阅读自己喜欢的歌曲的歌词。
特征
- 搜索功能:用户可以按歌曲标题和艺术家姓名搜索歌词。
- API 集成:从公共歌词 API 中获取歌词。
- 动态内容:根据用户输入动态显示歌词。
- 用户友好的界面:干净且易于使用的界面,用于搜索和查看歌词。
使用的技术
- React:用于构建用户界面和管理组件状态。
- CSS:用于设计应用程序的样式。
- JavaScript:用于处理 API 请求和应用程序逻辑。
项目结构
项目组织如下:
├── public ├── src │ ├── components │ │ ├── LyricsFinder.jsx │ │ ├── SearchForm.jsx │ ├── App.jsx │ ├── App.css │ ├── index.js │ └── index.css ├── package.json └── README.md
关键部件
- LyricsFinder.jsx:管理搜索逻辑并显示获取的歌词。
- SearchForm.jsx:提供一个表单供用户输入歌曲标题和艺术家姓名。
- App.jsx:渲染主布局和 LyricsFinder 组件。
代码说明
LyricsFinder 组件
LyricsFinder 组件处理 API 集成并管理搜索结果。
import { useState } from "react"; import SearchForm from "./SearchForm"; const LyricsFinder = () => { const [lyrics, setLyrics] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); const fetchLyrics = async (song, artist) => { setLoading(true); setError(""); try { const response = await fetch(`https://api.lyrics.ovh/v1/${artist}/${song}`); if (!response.ok) { throw new Error("Lyrics not found"); } const data = await response.json(); setLyrics(data.lyrics); } catch (err) { setError(err.message); } finally { setLoading(false); } }; return ( <div className="lyrics-finder"> <SearchForm onSearch={fetchLyrics} /> {loading && <p>Loading...</p>} {error && <p className="error">{error}</p>} {lyrics && <pre className="lyrics">{lyrics}}
该组件管理歌词、加载和错误消息的状态。它从 API 获取歌词并显示它们。
搜索表单组件
SearchForm 组件提供了一个表单供用户输入歌曲标题和艺术家姓名。
import { useState } from "react"; const SearchForm = ({ onSearch }) => { const [song, setSong] = useState(""); const [artist, setArtist] = useState(""); const handleSubmit = (e) => { e.preventDefault(); onSearch(song, artist); }; return ( <form onSubmit={handleSubmit} className="search-form"> <input type="text" placeholder="Song Title" value={song} onChange={(e) => setSong(e.target.value)} /> <input type="text" placeholder="Artist Name" value={artist} onChange={(e) => setArtist(e.target.value)} /> <button type="submit">Search</button> </form> ); }; export default SearchForm;
该组件接受用户输入的歌曲标题和艺术家并触发搜索功能。
应用程序组件
App 组件管理布局并渲染 LyricsFinder 组件。
import LyricsFinder from './components/LyricsFinder' import "./App.css" const App = () => { return ( <div className='app'> <div className="heading"> <h1>Lyrics Finder</h1> </div> <LyricsFinder/> <div className="footer"> <p>Made with ❤️ by Abhishek Gurjar</p> </div> </div> ) } export default App
该组件提供标题并在中心呈现 LyricsFinder 组件。
CSS 样式
CSS 设计应用程序以确保界面干净且用户友好。
* { box-sizing: border-box; } body { margin: 0; padding: 0; font-family: sans-serif; } .app { width: 100%; height: 100vh; background-image: url(./assets/images/bg.jpg); background-size: cover; background-repeat: no-repeat; display: flex; flex-direction: column; align-items: center; justify-content: center; } .heading { width: 200px; height: 40px; display: flex; align-items: center; margin-bottom: 20px; justify-content: center; background-color: #eab229; color: black; border: 2px solid white; border-radius: 20px; text-align: center; } .heading h1 { font-size: 18px; } .lyrics-container { margin-top: 10px; color: white; display: flex; align-items: center; flex-direction: column; } .input-container { display: flex; align-items: center; flex-direction: column; } .track-input-box { margin: 7px; width: 500px; height: 30px; background-color: #363636; border: 1.5px solid white; border-radius: 7px; overflow: hidden; } .track-input-box input { width: 480px; height: 30px; background-color: #363636; color: white; margin-left: 10px; outline: none; border: none; } .input-search { display: flex; align-items: center; justify-content: center; } .artist-input-box { margin: 7px; width: 400px; height: 30px; background-color: #363636; border: 1.5px solid white; border-radius: 7px; overflow: hidden; } .artist-input-box input { width: 380px; height: 30px; margin-left: 10px; background-color: #363636; color: white; border: none; outline: none; } .search-btn { width: 100px; padding: 6px; border-radius: 7px; border: 1.5px solid white; background-color: #0e74ad; color: white; font-size: 16px; } .search-btn:hover { background-color: #15557a; } .output-container { background-color: black; width: 600px; height: 300px; border: 1.5px solid white; border-radius: 7px; overflow-y: scroll; margin-block: 40px; } .output-container::-webkit-scrollbar { display: none; } .output-container p { margin: 30px; text-align: center; font-size: 16px; } .footer { font-size: 14px; color: white; }
样式确保布局简洁,具有用户友好的视觉效果和响应式设计。
安装与使用
要开始此项目,请克隆存储库并安装依赖项:
git clone https://github.com/abhishekgurjar-in/lyrics-finder.git cd lyrics-finder npm install npm start
这将启动开发服务器,并且应用程序将在 http://localhost:3000 上运行。
现场演示
在此处查看歌词查找器的现场演示。
结论
Lyrics Finder 项目是练习集成 API 和在 React 中处理动态内容的绝佳方法。它提供了一个使用干净且交互式的用户界面构建有用的应用程序的实际示例。
制作人员
- 灵感:该项目的灵感来自于在听音乐时快速访问歌词的需求。
作者
Abhishek Gurjar 是一位 Web 开发人员,热衷于构建交互式且引人入胜的 Web 应用程序。您可以在 GitHub 上关注他的工作。
以上是使用 React 构建歌词查找器应用程序的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Python更适合初学者,学习曲线平缓,语法简洁;JavaScript适合前端开发,学习曲线较陡,语法灵活。1.Python语法直观,适用于数据科学和后端开发。2.JavaScript灵活,广泛用于前端和服务器端编程。

JavaScript在Web开发中的主要用途包括客户端交互、表单验证和异步通信。1)通过DOM操作实现动态内容更新和用户交互;2)在用户提交数据前进行客户端验证,提高用户体验;3)通过AJAX技术实现与服务器的无刷新通信。

JavaScript在现实世界中的应用包括前端和后端开发。1)通过构建TODO列表应用展示前端应用,涉及DOM操作和事件处理。2)通过Node.js和Express构建RESTfulAPI展示后端应用。

理解JavaScript引擎内部工作原理对开发者重要,因为它能帮助编写更高效的代码并理解性能瓶颈和优化策略。1)引擎的工作流程包括解析、编译和执行三个阶段;2)执行过程中,引擎会进行动态优化,如内联缓存和隐藏类;3)最佳实践包括避免全局变量、优化循环、使用const和let,以及避免过度使用闭包。

Python和JavaScript在社区、库和资源方面的对比各有优劣。1)Python社区友好,适合初学者,但前端开发资源不如JavaScript丰富。2)Python在数据科学和机器学习库方面强大,JavaScript则在前端开发库和框架上更胜一筹。3)两者的学习资源都丰富,但Python适合从官方文档开始,JavaScript则以MDNWebDocs为佳。选择应基于项目需求和个人兴趣。

Python和JavaScript在开发环境上的选择都很重要。1)Python的开发环境包括PyCharm、JupyterNotebook和Anaconda,适合数据科学和快速原型开发。2)JavaScript的开发环境包括Node.js、VSCode和Webpack,适用于前端和后端开发。根据项目需求选择合适的工具可以提高开发效率和项目成功率。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。 1)C 用于解析JavaScript源码并生成抽象语法树。 2)C 负责生成和执行字节码。 3)C 实现JIT编译器,在运行时优化和编译热点代码,显着提高JavaScript的执行效率。

Python更适合数据科学和自动化,JavaScript更适合前端和全栈开发。1.Python在数据科学和机器学习中表现出色,使用NumPy、Pandas等库进行数据处理和建模。2.Python在自动化和脚本编写方面简洁高效。3.JavaScript在前端开发中不可或缺,用于构建动态网页和单页面应用。4.JavaScript通过Node.js在后端开发中发挥作用,支持全栈开发。
