React + Redux示例,实现商品增删改

目录结构

1.项目搭建

1.1 使用create-react-app react_redux创建项目

1.2 安装使用redux需要的依赖 npm install redux react-redux redux-devtools

2.添加一些文件夹

2.1创建储存常量的文件夹添加cart.js

  1. export const ADD_CART = "ADD_CART"
  2. export const UPDATE_CART = 'UPDATE_CART';
  3. export const DELETE_FROM_CART = 'DELETE_FROM_CART';

2.2创建action文件夹添加cart.js

  1. import { ADD_CART, UPDATE_CART, DELETE_FROM_CART } from '../contants/cart'
  2. export const addCart = function (product, quantity, unitCost) {
  3. return {
  4. type: ADD_CART,
  5. payload: { product, quantity, unitCost }
  6. }
  7. }
  8. export const updateCart = function (product, quantity, unitCost) {
  9. return {
  10. type: UPDATE_CART,
  11. payload: { product, quantity, unitCost }
  12. }
  13. }
  14. export const deleteFromCart = function (product) {
  15. return {
  16. type: DELETE_FROM_CART,
  17. payload: { product }
  18. }
  19. }

2.3创建reducers文件夹

2.3.1 cart.js

  1. import { ADD_CART, UPDATE_CART, DELETE_FROM_CART } from '../contants/cart'
  2. const initialState = {
  3. shops: [
  4. {
  5. product: '面包',
  6. quantity: 2,
  7. unitCost: 90
  8. },
  9. {
  10. product: '牛奶',
  11. quantity: 1,
  12. unitCost: 47
  13. }
  14. ]
  15. }
  16. const cartReducer = function (state = initialState, action) {
  17. switch (action.type) {
  18. case ADD_CART: {
  19. return {
  20. ...state,
  21. shops: [...state.shops, action.payload]
  22. }
  23. }
  24. case UPDATE_CART: {
  25. return {
  26. ...state,
  27. shops: state.shops.map(item => {
  28. item = item.product === action.payload.product ? action.payload : item
  29. return item
  30. })
  31. }
  32. }
  33. case DELETE_FROM_CART: {
  34. return {
  35. ...state,
  36. shops: state.shops.filter(item => item.product !== action.payload.product)
  37. }
  38. }
  39. default:
  40. return state
  41. }
  42. }
  43. export default cartReducer

2.3.2productsReducer.js

  1. const productsReducer = function (state = [], action) {
  2. return state
  3. }
  4. export default productsReducer

2.3.3index.js

  1. import { combineReducers } from 'redux'
  2. import cartReducer from './cart'
  3. import productsReducer from './productsReducer'
  4. const allReducers = {
  5. cart: cartReducer,
  6. products: productsReducer
  7. }
  8. const rootReducer = combineReducers(allReducers)//合并reducer
  9. export default rootReducer

2.4创建store

  1. import { createStore } from 'redux'
  2. import rootReducers from '../reducers'
  3. import { composeWithDevTools } from 'redux-devtools-extension';
  4. // composeWithDevTools 在控制台可以查看数据
  5. let store = createStore(rootReducers, composeWithDevTools())
  6. console.log("initial state: ", store.getState());
  7. export default store;

3.修改src下index.js

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import './index.css';
  4. import App from './App';
  5. import * as serviceWorker from './serviceWorker';
  6. import store from './store/index'
  7. import { Provider } from 'react-redux'
  8. ReactDOM.render(<Provider store={store}>
  9. <App />
  10. </Provider>, document.getElementById('root'));
  11. // If you want your app to work offline and load faster, you can change
  12. // unregister() to register() below. Note this comes with some pitfalls.
  13. // Learn more about service workers: https://bit.ly/CRA-PWA
  14. serviceWorker.unregister();

4.修改App.js

  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux'
  3. import './App.css';
  4. import * as cartActions from './actions/cart'
  5. class App extends Component {
  6. constructor(props) {
  7. super(props)
  8. }
  9. render() {
  10. let { shops } = this.props.cart;
  11. return (
  12. <div className="App">
  13. <ul>
  14. {shops.map((s, index) => {
  15. return <li key={index}>{s.product}===>{s.quantity}===>{s.unitCost}</li>
  16. })}
  17. </ul>
  18. <hr />
  19. <button onClick={() => this.props.addCart()} > 增加商品</button>
  20. <button onClick={() => this.props.updateCart()} > 修改商品</button>
  21. <button onClick={() => this.props.deleteCart()} > 删除商品</button>
  22. </div>
  23. )
  24. }
  25. }
  26. function getState(state) {
  27. return {
  28. cart: state.cart
  29. }
  30. }
  31. function getDispatch(dispatch) {
  32. return {
  33. addCart: () => dispatch(cartActions.addCart("鱼香肉丝", 1, 20)),
  34. updateCart: () => dispatch(cartActions.updateCart("鱼香肉丝", 2, 50)),
  35. deleteCart: () => dispatch(cartActions.deleteFromCart("鱼香肉丝"))
  36. }
  37. }
  38. export default connect(getState, getDispatch)(App);

初次使用redux做的小demo,记录一下

react+redux项目搭建及示例的更多相关文章

  1. Immutable.js 以及在 react+redux 项目中的实践

    来自一位美团大牛的分享,相信可以帮助到你. 原文链接:https://juejin.im/post/5948985ea0bb9f006bed7472?utm_source=tuicool&ut ...

  2. 用react+redux+webpack搭建项目一些疑惑

    --save-dev开发用 例如:webpack --save开发和发布用 例如:react

  3. node.js+ react + redux 环境搭建

    1.安装node.js 2. yarn init: 初始化,主要包含以下条目 name: 项目名 version: 版本号 description: 项目简要描述 entry point: 文件入口, ...

  4. React项目搭建与部署

    React项目搭建与部署 一,介绍与需求 1.1,介绍 1.1.1,React简介 React 是一个用于构建用户界面的 JAVASCRIPT 库. React主要用于构建UI,很多人认为 React ...

  5. react + redux 完整的项目,同时写一下个人感悟

    先附上项目源码地址和原文章地址:https://github.com/bailicangd... 做React需要会什么? react的功能其实很单一,主要负责渲染的功能,现有的框架,比如angula ...

  6. React:redux+router4搭建应用骨架

    可能是短期内关于react的对后一篇笔记.假设读者对redux和router4有基本了解. 缘由: 现在网上很多关于react+redux的文章都是沿用传统的文件组织形式,即: |--componen ...

  7. react 创建项目 sass router redux

    ​ 创建项目第一步 基本搭建 在创建之前,需要有一个git 仓库,我们要把项目搭建到git 中 目录介绍 cd 到某个盘 mkdir workspace 创建workspace文件夹 cd works ...

  8. React+Redux开发实战项目【美团App】,没你想的那么难

    README.md 前言 开始学习React的时候,在网上找了一些文章,读了官网的一些文档,后来觉得React上手还是蛮简单的, 然后就在网上找了一个React实战的练手项目,个人学完之后觉得这个项目 ...

  9. 用yeoman搭建react画廊项目笔记

    1.安装yeoman   npm install yo -g yo --version //检测 yeoman版本,成功显示版本号,则安装成功 2.到yeoman官网 http://yeoman.io ...

随机推荐

  1. Jenkins - 部署在Tomcat容器里的Jenkins,提示“反向代理设置有误”

    提示"反向代理设置有误"的背景 将jenkins.war放在tomcat容器中运行 访问Jenkins-系统管理,会提示"反向代理设置有误" 如何解决 在tom ...

  2. 【JVM第七篇】执行引擎

    写在前面的话:本文是在观看尚硅谷JVM教程后,整理的学习笔记.其观看地址如下:尚硅谷2020最新版宋红康JVM教程 执行引擎是Java虚拟机中的核心组成部分. 执行引擎的作用就是解析虚拟机字节码指令, ...

  3. linux之HTTP服务

    1.基本的配置   httpd:俗称Apache (A pachey Server) /etc/httpd/conf/httpd.conf            #配置文件 /etc/httpd/co ...

  4. 在线调整ceph的参数

    能够动态的进行系统参数的调整是一个很重要并且有用的属性 ceph的集群提供两种方式的调整,使用tell的方式和daemon设置的方式 一.tell方式设置 调整配置使用命令: 调整mon的参数 #ce ...

  5. vue统计组件库和ui框架

    UI组件 element ★13489 - 饿了么出品的Vue2的web UI工具套件 Vux ★8133 - 基于Vue和WeUI的组件库 iview ★6634 - 基于 Vuejs 的开源 UI ...

  6. 通过Folx的排序功能来设置下载任务的优先级

    当我们使用Folx进行多任务下载时,突然遇到要下载一个紧急文件的情况,该如何让这个紧急文件的下载任务排在优先的位置?当然,用户也可以先暂停所有的下载任务,仅开启紧急文件的下载任务. 但这种方式需要用户 ...

  7. 美食vlog如何剪辑?用什么视频制作软件剪辑比较好?

    是不是发现自己拍摄的美食永远没有美食博主拍出来的好看?那么美食vlog如何剪辑?用什么视频制作软件剪辑比较好呢?下面小编就教大家用视频编辑软件会声会影强大的颜色分级功能就能拯救你的美食vlog. 接下 ...

  8. 前端(web)知识-html

    前端由三部分组成: HTML(标签)--CSS(美化,修饰)--JS(执行指令) HTML(超文本标记语言,Hypertext Markup Language):是一种用于创建网页的标记语言. 本质上 ...

  9. JAVA8新特性Optional,非空判断

    Optional java 的 NPE(Null Pointer Exception)所谓的空指针异常搞的头昏脑涨, 有大佬说过 "防止 NPE,是程序员的基本修养." 但是修养归 ...

  10. leetcode151. 翻转字符串里的单词

    给定一个字符串,逐个翻转字符串中的每个单词. 示例 1:输入: "the sky is blue"输出: "blue is sky the"示例 2:输入: & ...