一篇文章教你用React實(shí)現(xiàn)菜譜系統(tǒng)
一、菜譜大全
1.1、項(xiàng)目背景
當(dāng)下回家吃飯健康飲食的理念正在興起。據(jù)調(diào)查顯示,有超過(guò)九成的都市白領(lǐng)及年輕人其實(shí)都傾向于在家里吃飯,尤其是有小孩的家庭意愿會(huì)更加強(qiáng)烈, 因?yàn)樗麄兤毡槎颊J(rèn)為在家里吃飯的幸福感會(huì)更高; 隨著經(jīng)濟(jì)的快速發(fā)展,人們的生活水平的逐漸提高,對(duì)飲食質(zhì)量要求也越來(lái)越高,但都市快節(jié)奏的生活讓上班族們吃飯的目標(biāo)性更小,通常只是到了時(shí)間隨機(jī)選擇食物塞飽肚子。該美食網(wǎng)站倡導(dǎo)一種全新的健康的生活方式,用戶可以根據(jù)網(wǎng)站上提供的食譜了解不同菜系的風(fēng)格、做法及搭配,除了可以查看各種食譜學(xué)習(xí)做飯, 還可以在線與其他用戶一起交流和分享做菜的心得,通過(guò)美食來(lái)感受生活之美。
1.2、技術(shù)棧
使用 React 框架來(lái)完成本次項(xiàng)目的實(shí)現(xiàn),使用技術(shù)有如下一些:
nodejs 進(jìn)行 模擬接口數(shù)據(jù) ( 代理 )
react react-dom
react-router-dom
redux react-redux redux-thunk immutable redux-immutable
styled-components ( css作用域 ) / sass / less / stylus
antd-mobile ui 組件庫(kù) ( 移動(dòng)端 )
react-transition-group
axios
http-proxy-middleware
配置裝飾器 ( costomize-cra react-app-rewired ) 等等
1.3、開(kāi)發(fā)環(huán)境
開(kāi)發(fā)環(huán)境為:Windows-
開(kāi)發(fā)工具:VSCode / webstorm + jsx 插件
開(kāi)發(fā)調(diào)試工具:Chrome 瀏覽器 react-devtools,redux-devtools
開(kāi)發(fā)運(yùn)行環(huán)境:node 環(huán)境
代碼管理:git
上線環(huán)境:linux + nginx
1.4、項(xiàng)目效果展示

1.5、項(xiàng)目初始化
- 在本機(jī)磁盤中指定位置創(chuàng)建一下 react 項(xiàng)目,命令如下
npx create-react-app cookbook
- 創(chuàng)建好項(xiàng)目后,進(jìn)入項(xiàng)目目錄先安裝常規(guī)要使用的三方包,命令如下
npm i -D customize-cra react-app-rewired http-proxy-middleware
npm i -S redux react-redux redux-thunk styled-components immutable redux-immutable react-router-dom react-transition-group axios
- 清理創(chuàng)建好的項(xiàng)目中不需要的文件及文件夾
1.刪除 public 目錄下的部份內(nèi)容
2.刪除 src 目錄下的部份內(nèi)容
- 修改 public / index.html
- 在 src 目錄下創(chuàng)建 根組件 App.jsx 與項(xiàng)目 入口文件 index.js
- 配置 裝飾器 支持

// 此文件就是對(duì)于 webpack 進(jìn)行增量配置 它是運(yùn)行在 nodejs 中的 commonjs
const { resolve } = require('path')
// 增量對(duì)于本項(xiàng)目中的 webpack 配置進(jìn)行修改 和 添加操作類
const { addDecoratorsLegacy, override } = require('customize-cra')
// 自定義 webpack 配置
const customize = () => config => {
// 給當(dāng)前項(xiàng)目添加一個(gè) @ 字符串,來(lái)方便寫(xiě)代碼時(shí)的導(dǎo)入路徑
config.resolve.alias['@'] = resolve('src')
return config
}
// 導(dǎo)出
module.exports = override(
// 添加裝飾器支持
addDecoratorsLegacy(),
// 添加自定義 webpack 配置
customize()
)

- 修改
package.json中的腳本命令為如下

"scripts": {
"start": "set BROWSER=NONE && react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
- 配置 反向代理

// create-react-app 腳本架工具提供一個(gè)專門用來(lái)進(jìn)行代理配置的文件,它是給 nodejs 運(yùn)行
// 模塊化使用 commonjs 規(guī)范
// create-react-app 腳本架工具只提供了這個(gè)入口,但是代理操作沒(méi)有幫你完成
// 需要手動(dòng)去安裝第 3 方包來(lái)實(shí)現(xiàn) 代理
// npm i -D http-proxy-middleware
// 修改此文件后,一定要 => 重啟服務(wù)
const { createProxyMiddleware: proxy } = require('http-proxy-middleware');
// app 對(duì)象就是 express 對(duì)象
module.exports = app => {
app.use(
'/api',
proxy({
target:'https://api.iynn.cn/film',
changeOrigin:true,
}))
}
項(xiàng)目已在本機(jī)中 初始化 完成,需要在 遠(yuǎn)程 倉(cāng)庫(kù)中創(chuàng)建 git 倉(cāng)庫(kù)本機(jī)把項(xiàng)目初始化一個(gè) git 倉(cāng)庫(kù)

提交到遠(yuǎn)程后,在本機(jī)開(kāi)始用 分支 來(lái)進(jìn)行,不要在 master 中進(jìn)行開(kāi)發(fā),切記
二、首頁(yè)開(kāi)發(fā)
2.1、antd-mobile 組件庫(kù)
在線文檔:Ant Design Mobile | A Mobile Design Specification
antd-mobile 是 Ant Design 的移動(dòng)規(guī)范的 React 實(shí)現(xiàn),服務(wù)于 螞蟻金服 及 口碑無(wú)線 業(yè)務(wù)。它支持多平臺(tái),組件豐富、能全面覆蓋各類場(chǎng)景,UI 樣式高度可配置,拓展性更強(qiáng),輕松適應(yīng)各類產(chǎn)品風(fēng)格。
- 在使用前需要先對(duì)包進(jìn)行安裝
npm i -S antd-mobile
- 按需加載 組件代碼 和 樣式的 babel 插件
// 按需加載 tree-shaking
npm i -D babel-plugin-import
// config-overrides.js 用于 修改默認(rèn)配置
const { override, fixBabelImports } = require('customize-cra')
module.exports = override(
fixBabelImports('import', {
libraryName: 'antd-mobile',
style: 'css',
})
)
- 使用
import React, { Component } from "react";
// 引入 `antd-mobile` 的 按鈕組件
import { Button } from "antd-mobile";
class App extends Component {
render() {
return (
<>
<Button type="primary">我是一個(gè)常規(guī)按鈕</Button>
</>
);
}
}
export default App;
- 樣式重置
移動(dòng)站點(diǎn)中的樣式,單位有兩種 rem / vw、vh
2.2、底部導(dǎo)航實(shí)現(xiàn)
參考地址:https://mobile.ant.design/components/tab-bar-cn/
底部導(dǎo)航 可以使用 antd-mobile 中的 tab-bar 組件完成此功能展示。

路由規(guī)劃
2.3、菜譜大全頂部導(dǎo)航
height: .4rem; line-height: .4rem; background: #FF6C0C; font-size: .18rem; text-align: center; color:#fff;
2.4、輪播顯示
參考地址:Ant Design Mobile | A Mobile Design Specification
該功能可以使用 antd-mobile 中的 Carousel 組件
2.5、mock 數(shù)據(jù)
mock數(shù)據(jù)( faker數(shù)據(jù) ),即使用假數(shù)據(jù)來(lái)模擬后臺(tái)的數(shù)據(jù)。
為什么要做假數(shù)據(jù)?因?yàn)?strong>后端開(kāi)發(fā)接口并產(chǎn)出接口文檔沒(méi)有那么快,此時(shí)就需要我們自己來(lái)模擬請(qǐng)求數(shù)據(jù)。模擬的數(shù)據(jù)字段、格式等,需要和后端工程師溝通。這樣,我們可以先通過(guò)模擬的數(shù)據(jù)繼續(xù)完成前端的工作任務(wù),待后端工程師寫(xiě)好數(shù)據(jù)接口并提供了接口信息后,我們只需要修改請(qǐng)求地址,前后端就實(shí)現(xiàn)了無(wú)縫銜接。
- 安裝 json-server 幫助我們快速啟動(dòng)一個(gè) web 服務(wù)
npm i -g json-server === yarn add global json-server
// json-server 默認(rèn)的路由模式
// 支持 get / post / put / delete 而且還支持文件寫(xiě)入 它是跨域的
// data.json
// http 請(qǐng)求地址 http://xxx/data
{
"data": {
"id": 1,
"name": "aaa",
"age": 20
}
}
json-server data.json
- mock數(shù)據(jù)

2.6、搜索組件
export const SearchBox = styled.div`
width: 90vw;
height: .46rem;
display: flex;
border: 1px solid #ff6c0c;
margin: .15rem auto;
border-radius: 5px;
box-shadow: 1px 1px 5px #ccc;
justify-content: center;
align-items: center;
img{
width: .2rem;
height: .2rem;
}
span{
color:#555;
margin-left: .1rem;
}
`
2.7、熱門分類
export const HotCateBox = styled.div`
background: #fff;
.title{
padding: .15rem;
color:#949494;
}
`
<Grid data={hotcate}
columnNum={3}
itemStyle={{ height: '.5rem' }}
onClick={(row, index) => {
console.log(index, this.props.history.push)
}}
renderItem={dataItem => (
<div>{dataItem.title}</div>
)}
/>
2.8、精品好菜
靜態(tài)布局展示
- HTML
<div> <h1>精品好菜</h1> <div> <dl> <dt> <img src="http://www.mobiletrain.org/images/index/new_logo.png" /> </dt> <dd> <h3>青椒香干</h3> <p>1000瀏覽 2000收藏</p> </dd> </dl> </div> </div>
- css
div {
padding-left: .1rem;
>h1 {
height: .5rem;
line-height: .6rem;
padding-left: .15rem;
color: #666;
padding-left: 0;
}
>div {
display: flex;
flex-wrap: wrap;
>dl {
width: calc(50% - 0.1rem);
background: #fff;
margin-right: .1rem;
margin-bottom: .1rem;
dt {
img {
width: 100%;
}
}
dd {
display: flex;
flex-direction: column;
align-items: center;
padding: .1rem;
h3 {
font-size: .16rem;
}
p {
font-size: .12rem;
color: #666;
}
}
}
}
三、分類開(kāi)發(fā)
3.1、分類頂部切換
創(chuàng)建需要的組件 和 樣式
- HTML
<ul> <li>分類</li> <li className="active">食材</li> <li className="slider right"></li> </ul>
- CSS
height:.44rem;
background:#ee742f;
display:flex;
align-items:center;
justify-content:center;
ul {
width: 1.4rem;
height: .3rem;
display: flex;
position: relative;
border: 1px solid #fff;
z-index: 0;
border-radius: .15rem;
li {
flex: 1;
line-height: .3rem;
text-align: center;
color: #fff;
&:last-child {
position: absolute;
width: 50%;
height: .3rem;
background: #fff;
z-index: -1;
border-radius: .15rem;
transform: translate(0, 0);
transition: all 0.4s ease-in;
&.right {
transform: translate(100%, 0);
}
}
&.active {
color: #ee742f;
}
}
3.2、列表展示
- HTML
<div> <div> <ul> <li class="active"><span>分類</span></li> </ul> </div> <div> <ul> <li>內(nèi)容</li> </ul> </div> </div>
- CSS
.div {
height: 100%;
display: flex;
>div:first-child {
width: .9rem;
>ul {
height: 100%;
overflow-y: scroll;
li {
height: .5rem;
text-align: center;
line-height: .5rem;
background: #f3f3f3;
&.active {
background: #fff;
span {
display: inline-block;
height: 100%;
border-bottom: 1px solid #ee742f;
}
}
}
}
}
>div:last-child {
flex: 1;
background: #fff;
padding: .2rem .1rem;
>ul {
display: flex;
flex-wrap: wrap;
overflow-y: scroll;
height: 100%;
align-content: flex-start;
li {
width: 33.3333%;
text-align: center;
height: .5rem;
line-height: .5rem;
color: #666;
}
}
}
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
React在定時(shí)器中無(wú)法獲取狀態(tài)最新值的問(wèn)題
這篇文章主要介紹了React在定時(shí)器中無(wú)法獲取狀態(tài)最新值的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
React實(shí)現(xiàn)錨點(diǎn)跳轉(zhuǎn)組件附帶吸頂效果的示例代碼
這篇文章主要為大家詳細(xì)介紹了React如何實(shí)現(xiàn)移動(dòng)端錨點(diǎn)跳轉(zhuǎn)組件附帶吸頂效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-01-01
react中的useImperativeHandle()和forwardRef()用法
這篇文章主要介紹了react中的useImperativeHandle()和forwardRef()用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
react-router?v6實(shí)現(xiàn)動(dòng)態(tài)路由實(shí)例
這篇文章主要為大家介紹了react-router?v6實(shí)現(xiàn)動(dòng)態(tài)路由實(shí)例詳解,<BR>有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
react項(xiàng)目如何運(yùn)行在微信公眾號(hào)
這篇文章主要介紹了react項(xiàng)目如何運(yùn)行在微信公眾號(hào),幫助大家更好的理解和學(xué)習(xí)使用react,感興趣的朋友可以了解下2021-04-04
詳解React Native開(kāi)源時(shí)間日期選擇器組件(react-native-datetime)
本篇文章主要介紹了詳解React Native開(kāi)源時(shí)間日期選擇器組件(react-native-datetime),具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09

