
在本教程中,我们将使用 react 创建一个 lyrics finder web 应用程序。该项目非常适合那些想要练习集成 api、管理状态和显示动态内容的人。
歌词查找器允许用户通过输入歌曲标题和艺术家姓名来搜索歌词。它从公共 api 获取歌词并将其显示在屏幕上。用户可以快速找到并阅读自己喜欢的歌曲的歌词。
项目组织如下:
├── public ├── src │ ├── components │ │ ├── lyricsfinder.jsx │ │ ├── searchform.jsx │ ├── app.jsx │ ├── app.css │ ├── index.js │ └── index.css ├── package.json └── readme.md
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}</pre>}
</div>
);
};
export default lyricsfinder;
该组件管理歌词、加载和错误消息的状态。它从 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 设计应用程序以确保界面干净且用户友好。
* {
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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号