使用 React 构建歌词查找器应用程序

老强小哥_8667

老强小哥_8667

2024-09-10

544人浏览

转载

使用 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}"></searchform>
      {loading && <p>loading...</p>}
      {error && <p classname="error">{error}</p>}
      {lyrics && <pre class="brush:php;toolbar:false;" classname="lyrics">{lyrics}
} ); }; 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 (
    
setsong(e.target.value)} /> setartist(e.target.value)} />
); }; 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></lyricsfinder><div classname="footer">
        <p>made with ❤️ by abhishek gurjar</p><div class="aritcle_card flexRow artxards">
											<div class="artcardd flexRow">
												<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill12915" title="git-push"><img
														src="https://img.php.cn/upload/skill/001/246/273/6adaf20e695c2374.jpg" alt="git-push" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
												<div class="aritcle_card_info flexColumn">
													<a rel="nofollow" href="/xiazai/skill12915" title="git-push" class="overflowclass">git-push</a>
													<p class="overflowclass">自动用 git 提交代码并推送到远程仓库。当用户输入 `/gitpush` 时触发此技能。功能包括:自动暂存所有更改、自动生成提交信息、自动提交到本地仓库、自动推送到远程分支。成功后报告提交状态、文件数量和耗时。</p>
												</div>
												<a rel="nofollow" href="/xiazai/skill12915" title="git-push" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
												</a>
											</div>
										</div>
      </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 上关注他的工作。

相关文章

PHP速学视频免费教程(入门到精通)
PHP速学视频免费教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

css git overflow

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
github中文官网入口 github中文版官网网页进入
github中文官网入口 github中文版官网网页进入

github中文官网入口https://docs.github.com/zh/get-started,GitHub 是一种基于云的平台,可在其中存储、共享并与他人一起编写代码。 通过将代码存储在GitHub 上的“存储库”中,你可以: “展示或共享”你的工作。 持续“跟踪和管理”对代码的更改。

2026.01.21

8965

27

GitHub官网入口版本汇总
GitHub官网入口版本汇总

本专题整合了GitHub入口版本地址汇总,阅读专题下面的文章了解更多详细内容。

2026.04.02

1058

19

http500解决方法
http500解决方法

http500解决方法有检查服务器日志、检查代码错误、检查服务器配置、检查文件和目录权限、检查资源不足、更新软件版本、重启服务器或寻求专业帮助等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

2023.11.09

4183

4

http请求415错误怎么解决
http请求415错误怎么解决

解决方法:1、检查请求头中的Content-Type;2、检查请求体中的数据格式;3、使用适当的编码格式;4、使用适当的请求方法;5、检查服务器端的支持情况。更多http请求415错误怎么解决的相关内容,可以阅读下面的文章。

2023.11.14

1014

3

HTTP 503错误解决方法
HTTP 503错误解决方法

HTTP 503错误表示服务器暂时无法处理请求。想了解更多http错误代码的相关内容,可以阅读本专题下面的文章。

2024.03.12

6986

8

http与https有哪些区别
http与https有哪些区别

http与https的区别:1、协议安全性;2、连接方式;3、证书管理;4、连接状态;5、端口号;6、资源消耗;7、兼容性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

2024.08.16

5630

6

Go 语言 HTTP 编程与中间件开发教程合集
Go 语言 HTTP 编程与中间件开发教程合集

全面讲解 Go 语言 HTTP 编程的核心知识,涵盖 net/http 标准库的 Server/Handler/HandlerFunc 体系、DefaultServeMux 路由注册与自定义路由器、Request / ResponseWriter 请求响应处理、中间件(Middleware)链式设计模式与常见中间件(日志/CORS/认证/限流)开发、http.Client 的超时配置与连接池管理、Cookie / Session 处理、

2026.05.08

621

31

GoFrame框架Web开发与路由中间件
GoFrame框架Web开发与路由中间件

本专题聚焦GoFrame框架的Web开发,拆解路由注册与匹配规则、请求参数获取、参数校验与中间件执行顺序,说明统一响应、JWT鉴权、文件上传与接口文档生成的落地方式,并补充优雅重启与测试实践。

2026.11.17

0

15

GoFrame框架安装与gf命令行工具
GoFrame框架安装与gf命令行工具

本专题围绕GoFrame框架的安装与工具链配置展开,讲清Go版本要求、gf命令行的安装方式、代理镜像与依赖准备,说明项目初始化、热编译、交叉编译打包的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.17

0

15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程