• Stars
    star
    171
  • Rank 222,266 (Top 5 %)
  • Language
    JavaScript
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

👞基于react的购物车实战项目

前言

前些日子抽空学习了下react,因为近期忙着找工作,没时间写博客,今天我们就来看看用react全家桶,构建一个项目把,可能我学的也不是特别好,但是经过各种查资料,总算是能够构建出一个像模像样的栗子了。

github地址:https://github.com/hua1995116/react-shopping

脚手架

generator-react-webpack

Installation

npm install -g yo
npm install -g generator-react-webpack

Setting up projects

# Create a new directory, and `cd` into it:
mkdir my-new-project && cd my-new-project
 
# Run the generator
yo react-webpack

技术栈

react+react-router+redux+ webpack + ES6 + fetch+antd

项目结构

│  .babelrc
│  .editorconfig
│  .eslintrc
│  .gitignore
│  .yo-rc.json
│  karma.conf.js
│  package.json
│  prod.server.js
│  server.js
│  shop.json
│  tree.txt
│  webpack.config.js
│  
├─cfg
│      base.js
│      defaults.js
│      dev.js
│      dist.js
│      test.js
│      
├─dist
│          
├─src
│  │  favicon.ico
│  │  index.html
│  │  index.js
│  │  routes.js
│  │  
│  ├─actions
│  │      index.js
│  │      README.md
│  │      
│  ├─api
│  │      shop.json
│  │      
│  ├─components
│  │      Destination.js
│  │      Detail.js
│  │      Index.js
│  │      Main.js
│  │      Plan.js
│  │      
│  ├─config
│  │      base.js
│  │      dev.js
│  │      dist.js
│  │      README.md
│  │      test.js
│  │      
│  ├─constants
│  │      ActionTypes.js
│  │      
│  ├─images
│  │      
│  ├─reducers
│  │      cart.js
│  │      count.js
│  │      history.js
│  │      index.js
│  │      
│  ├─sources
│  │      
│  ├─stores
│  │      
│  └─styles
│          App.css
│          
└─test
            

目标功能

  • 商品浏览页面 -- 完成
  • 商品详细页面-- 完成
  • 购物车页面-- 完成
  • 历史记录页面 -- 完成

项目运行

注意:由于涉及大量的 ES6 等新属性,nodejs 必须是 6.0 以上版本 。

git clone https://github.com/hua1995116/react-shopping.git 

cd react-shopping

npm install

npm run start //运行

npm run dist //打包

说明

如果本项目对于你有帮助,请顺手进github点个star

该项目已经在windows 7和mac进行测试。

推荐一个vue2 的实战项目(仿网易云音乐) http://blog.csdn.net/blueblueskyhua/article/details/68156734

另外推荐一个 vue2 + vuex 的实战项目(实时聊天系统,有后台代码)。http://blog.csdn.net/blueblueskyhua/article/details/70807847

如果有什么更好的建议或者问题,请及时再下方评论留言。

效果演示

http://www.huayifeng.top:8080

核心代码说明

"react": "^15.0.0" 原本的 react package 被拆分为 react 及 react-dom 两个 package 详细看官方api: https://facebook.github.io/react/

"react-router": "^4.1.1" React Router V4 基于 Lerna 管理多个 Repository。在此代码库包括:

  • react-router React Router 核心
  • react-router-dom 用于 DOM 绑定的 React Router
  • react-router-native 用于 React Native 的 React Router
  • react-router-redux React Router 和 Redux 的集成
  • react-router-config 静态路由配置帮助助手 api :https://reacttraining.com/react-router/web/guides/quick-start

src/index.js

import 'core-js/fn/object/assign'
import React from 'react'
import ReactDOM from 'react-dom' // 14.0版本后拆分成react和react-demo
import { createStore ,applyMiddleware } from 'redux'
import { Provider } from 'react-redux'
import Main from './components/Main'
import { createLogger } from 'redux-logger'
import thunk from 'redux-thunk'
import reducer from './reducers'
import 'antd/dist/antd.css'

import './styles/App.css'
import { getAllProducts } from './actions'

const middleware = [ thunk ] // redux-thunk解决异步回调
if (process.env.NODE_ENV !== 'production') {
  middleware.push(createLogger())
}

const store = createStore(reducer,
  applyMiddleware(...middleware) // 中间件
)

store.dispatch(getAllProducts()) //获取全部商品
// Render the main component into the dom
ReactDOM.render(
   <Provider store={ store } >
     <Main />
   </Provider>
  ,document.getElementById('app')
)

主要定义了一些依赖。以及主入口模版文件Main.js

src/components/Main.js

import React from 'react'
import {
  BrowserRouter as Router,
  Route,
  Link
} from 'react-router-dom'
import {connect} from 'react-redux'
import Index from './index'
import Destination from './Destination'
import Plan from './Plan'
import Detail from './Detail'
import {Menu, Icon} from 'antd'
const SubMenu = Menu.SubMenu

const Basic = () => (

  <Router >
    <div className="clear container-main">
      <div className="fl">
        <Menu
          style={{width: 240}}
          defaultSelectedKeys={['1']}
          defaultOpenKeys={['sub1']}
          mode="inline"
        >
          <SubMenu key="sub1" title={<span><Icon type="mail"/><span>操作</span></span>}>
            <Menu.Item key="1"><Link to="/">主页</Link></Menu.Item>
            <Menu.Item key="2"><Link to="/about">购物车</Link></Menu.Item>
            <Menu.Item key="3"><Link to="/topics">购买记录</Link></Menu.Item>
          </SubMenu>
        </Menu>
      </div>

      <Route exact path="/" component={Index}/>
      <Route path="/about" component={Destination}/>
      <Route path="/topics" component={Plan}/>
      <Route path="/detail/:topicId" component={Detail} />
    </div>
  </Router>
)

export default connect()(Basic)

运用了函数式编程方式: 我们可以看看普通继承和函数式编程的差异,函数编程简洁了不少。也可以看到react-router在4.0版本后发生了一些变化。 这里写图片描述

效果图

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

More Repositories

1

awesome-ai-painting

AI绘画资料合集(包含国内外可使用平台、使用教程、参数教程、部署教程、业界新闻等等) stable diffusion tutorial、disco diffusion tutorial、 AI Platform
9,971
star
2

webchat

🔈 Websocket project based on vue(基于vue2.0的实时聊天项目)
JavaScript
1,951
star
3

react-resume-site

木及简历,一款markdown的在线简历工具。 https://www.mujicv.com
TypeScript
1,798
star
4

musiccloudWebapp

🎧vuejs仿网易云音乐
Vue
727
star
5

360-sneakers-viewer

鸿星尔克全景(360°)鞋子展示(包含建模过程)
HTML
275
star
6

node-demo

所有实战项目示例
HTML
217
star
7

google-translate-open-api

A free and unlimited API for Google Translate(support single text and Multi-segment text) 💵🚫
TypeScript
217
star
8

linec

🍬一个高颜值命令行统计代码行数的计数器。(counts lines of code)
JavaScript
120
star
9

mmt

A tool that automatically opens multiple panes to execute multiple commands in MacOS + iTerm2
TypeScript
62
star
10

git-down-repo

:octocat:Download git-repo for any url
JavaScript
54
star
11

shark-cleaner

A cleaning tool to help you quickly clear cache garbage.
JavaScript
50
star
12

Fly-Three.js

Three.js 起飞系列
JavaScript
47
star
13

mcharts

📝一个前端监控系统。
JavaScript
45
star
14

easy-json-parse

Parse your json safely and easily.
TypeScript
32
star
15

shaoshupai-trending-hot-search

少数派热门榜,记录从 2022-01-09 日开始的少数派热门文章。每天 21:00 抓取,按天归档。
JavaScript
21
star
16

npm-stat

download statistics for npm/cnpm packages
TypeScript
20
star
17

vuepress-blog

JavaScript
18
star
18

proxy

http-porxy 源码解析以及实现
JavaScript
16
star
19

myblog

a blog for hua1995116.
HTML
12
star
20

node-crawler

基于nodejs的爬虫
JavaScript
12
star
21

awesome-WebVR

Resources of WebVR(includes demo、framework and articles ) 关于 webVR 相关资源、咨询、工具、框架、平台。
8
star
22

vue

vue学习项目例子
JavaScript
8
star
23

shuhui

PHP基于web的书法设计作品网站
JavaScript
7
star
24

LRU-node-addon

A native module for Node supporting LRU (least-recently-used).
C++
7
star
25

tiny-webpack

A tiny webpack.
TypeScript
6
star
26

hua1995116

4
star
27

debug

debug demo
JavaScript
4
star
28

translate-md-viewer

This is a chrome extension to solve problem that people look at English article hardly.
JavaScript
3
star
29

timing-browser

前端监控-浏览器端
JavaScript
3
star
30

packaging-example

JavaScript
3
star
31

package-io

🛠package.json io
TypeScript
3
star
32

dark-mode

example for dark mode with vue3 and vite
Vue
2
star
33

Havetrip

JavaScript
2
star
34

apicloud-aistudy

a new project
HTML
2
star
35

cv-design

Vue
2
star
36

websocket-ws

a websocket sdk
JavaScript
2
star
37

vscode-daily-line

Calculate your daily programming time and number of lines
TypeScript
2
star
38

webpack-plugin-inner-script

a plugin for webpack
JavaScript
2
star
39

mmt-site

mmt site
TypeScript
2
star
40

GPTsEasy

Fint GTPs Easy
2
star
41

renting-tool

a tool for renting
JavaScript
1
star
42

fengchao

24号
CSS
1
star
43

shadow

JavaScript
1
star
44

sass-benchmark

Benchmark for Dart Sass sync/async and Node Sass
SCSS
1
star
45

node-tools

node-tools
JavaScript
1
star
46

svelte-ts-tailwind-vite-template

a template for svelte + ts + tailwind + vite.
Svelte
1
star
47

github-space

EJS
1
star
48

Casper-theme

ghost theme
CSS
1
star
49

newyear2018

new year for 2018
JavaScript
1
star
50

Zero

Mobile UI for Vue.js
1
star
51

ip-search

a service for search ip
Go
1
star
52

bifrost

bifrost
JavaScript
1
star
53

bamboo

JavaScript
1
star
54

jwt

a simple instance for jwt.
TypeScript
1
star
55

qushequ

qushequ
Java
1
star
56

auto-eating

1
star
57

2019-nCoV

CSS
1
star
58

eleme-vuejs

vuejs-1.0
Vue
1
star
59

UploadLoading

UploadLoading
HTML
1
star
60

leetcode

practice for arithmetic
HTML
1
star
61

common-comment-parser

A universal comment parser (supports more than 30 languages)
TypeScript
1
star
62

timing-receive

前端监控接受端
JavaScript
1
star
63

react-system

一个前端发布系统
JavaScript
1
star
64

react-system-node

The end of react-system
JavaScript
1
star
65

demo-publish-02

demo2 for publish
JavaScript
1
star
66

vue-flex-touch

A Vue.js directive both for long press touch on mobile and right click on pc.
Vue
1
star
67

rsa

rsa
JavaScript
1
star
68

awesome-comfyui

1
star
69

vue-online-editor

JavaScript
1
star
70

plus-in

JavaScript
1
star
71

google-translate-open-api-demo

Objective-C
1
star
72

vue-quick-scroll

a plugin for vue.
Vue
1
star