Day 26-制作购物车之设定Redux: reducers&store

主要呈现实作成果

以下内容有参考教学影片,底下有附网址。
(内容包括我的不专业解说分析及在实作过程中遇到的困难与解决)

go go~/images/emoticon/emoticon08.gif


今天要来设定Redux,在src资料夹中,新增redux资料夹
然後在redux资料夹里,先新增一个store.js档,接着新增三个资料夹分别为
->reducers、actions、constants
!小补充!
store:注意一个Redux应用只会有一个store。
constants:是常数的部分。
actions:传递资料到store里。
reducers:想要将资料处理拆分时使用。(而不是使用多个store)

store.js:
https://ithelp.ithome.com.tw/upload/images/20210928/20139720hKuMQUEwog.png
-Redux-Thunk:简单说就是能从一个function中,呼叫另一个function。
-import {createStore} from 'redux':建立store。
-applyMiddleware:能将多个middleware结合。
-combineReducers:随着程序越来越庞大,可能会将reducer分配到各处,这时就需要combineReducers。

index.js:
https://ithelp.ithome.com.tw/upload/images/20210928/20139720eYPHfM6TTJ.png
-provider:让任何有需要store的组件(components),能够允许使用。

###Constants
在constants资料夹里新增cartConstants.js档&productConstants.js档
cartConstants.js:
https://ithelp.ithome.com.tw/upload/images/20210928/201397208znzbdZSAM.png
输出ADD_TO_CART&REMOVE_FROM_CART&CART_RESET

productConstants.js:
https://ithelp.ithome.com.tw/upload/images/20210928/20139720zGJoKqTOhm.png
输出GET_PRODUCTS_REQUEST&GET_PRODUCTS_SUCCESS&GET_PRODUCTS_FAIL
GET_PRODUCTS_DETAIL_REQUEST&GET_PRODUCTS_DETAIL_SUCCESS&GET_PRODUCTS_DETAIL_FAIL

###Reducers
在reducers资料夹里新增cartReducers.js档
https://ithelp.ithome.com.tw/upload/images/20210928/20139720fheqNcWoga.png

###Actions
在actions资料夹里新增cartActions.js档
https://ithelp.ithome.com.tw/upload/images/20210929/20139720KzLXxPBaAi.png


参考教学网站:https://www.youtube.com/watch?v=0divhP3pEsg&t=786s
更多actions、reducers:https://chentsulin.github.io/redux/docs/basics/Store.html


  • 明天讲redux:actions/images/emoticon/emoticon29.gif

<<:  Spring Framework X Kotlin Day 23 Integration Test

>>:  有空再说与 TODO

Day15 - 产品编辑 modal 还丑但功能 OK 了

<template> <!-- Button trigger modal --&...

Day 2:Kotlin 程序设计基础入门 (1)

本篇文章同步发表在 HKT 线上教室 部落格,线上影音教学课程已上架至 Udemy 和 Youtu...

【D25】 说明一下bid and ask

前言 说明一下bid and ask是什麽,我们这边可以怎麽使用呢? 参考网站:[Bid and A...

Day14 突如其来的Minecraft

通常有玩过线上游戏的工程师都会有个小小的梦想,是自己能架个私服跟朋友们一起玩乐,前阵子因为疫情的缘故...

Day 11: 人工神经网路初探 基础知识建立

前篇我们介绍了关於机器学习的基础名词以及分类总集,接下来就让我们细讲人工神经网路的运作原理及基础结构...