[Functional Programming ADT] Initialize Redux Application State Using The State ADT
Not only will we need to give our initial state to a Redux store, we will also need to be able to reset our state at any time by dispatching an action. We can get the best of both worlds by having a function that will return an object with all of our initial values in it. Then use that function to craft a State ADT transition that will throw out whatever our previous state was and replace it with the original initial state.
We’ll not only build out an initialize state transaction, but also use that new transaction to craft an action creator to expose a means to dispatch at any time an action that will reset our state.
Set initial state:
We use PUT state to reset the state.
import State from "crocks/State";
const { put } = State;
// initialState :: () -> AppState
export const initialState = () => ({
colors: ["orange", "green", "blue", "yellow"],
shapes: ["triangle", "circle", "square"],
cards: [],
hint: {},
isCorrect: null,
left: ,
moves: ,
rank: ,
seed:
});
// initialize :: () -> State AppState ()
const initialize = () => put(initialState());
export default initialize;
Create action for reducer:
1. Create action const string
2. Action creator
3. Create reducer, bind action const to state ()
import { createAction, createReducer } from "../helpers";
import start, { markCardsUnselected } from "../model/game";
import initialize from "../model/initialize";
const HIDE_ALL_CARDS = "HIDE_ALL_CARDS";
const START_GAME = "START_GAME";
const RESET_GAME = "RESET_GAME";
// hideAllCards :: String -> Action String
export const hideAllCards = createAction(HIDE_ALL_CARDS);
// startGame :: String -> Action String
export const startGame = createAction(START_GAME);
// startGame :: String -> Action String
export const resetGame = createAction(RESET_GAME);
// reducer :: Reducer
const reducer = createReducer({
HIDE_ALL_CARDS: markCardsUnselected,
START_GAME: start,
RESET_GAME: initialize
});
export default reducer;
Kick off:
Call the reducer with state, action.
import log from "./logger";
import reducer from "./data/reducers";
import { resetGame } from "./data/reducers/game";
import initialize from "./data/model/initialize";
log(reducer({}, resetGame()));
[Functional Programming ADT] Initialize Redux Application State Using The State ADT的更多相关文章
- [Functional Programming] Pull Many Random Numbers in a Single State ADT Transaction
We have the ability to select a single random card from a pile of twelve cards, but we would like to ...
- [Functional Programming] Using JS, FP approach with Arrow or State Monad
Using Naive JS: const {modify, get} = require('crocks/State'); const K = require('crocks/combinators ...
- [Functional Programming Monad] Refactor Stateful Code To Use A State Monad
When we start to accumulate functions that all work on a given datatype, we end up creating a bunch ...
- [Functional Programming] Randomly Pull an Item from an Array with the State ADT (Pair)
Functor composition is a powerful concept that arises when we have one Functor nested in another Fun ...
- [Functional Programming] Transition State based on Existing State using the State ADT (liftState, composeK)
While sometimes outside input can have influence on how a given stateful transaction transitions, th ...
- [Functional Programming] Introduction to State, thinking in State
Recently, I am learning Working with ADT. Got some extra thought about State Monad. Basiclly how to ...
- [Functional Programming Monad] Combine Stateful Computations Using A State Monad
The true power of the State ADT really shows when we start combining our discrete, stateful transact ...
- [Functional Programming + React] Provide a reasonable default value for mapStateToProps in case initial state is undefined
For example we have a component, it needs to call 'react-redux' connect function. import { compose, ...
- [Functional Programming Moand] Update The State Of A State Monad (put)
Stateful computations require the ability for their state to change overtime. We take a look on one ...
随机推荐
- 六十 数据库访问 使用SQLAlchemy
数据库表是一个二维表,包含多行多列.把一个表的内容用Python的数据结构表示出来的话,可以用一个list表示多行,list的每一个元素是tuple,表示一行记录,比如,包含id和name的user表 ...
- python模块安装路径
Unix(Linux): prefix/lib/pythonX.Y/site-packages 默认路径:/usr/local/lib/pythonX.Y/site-packages 另外,在Unix ...
- Kbengine cocos2djs 地图问题
KBEngine.addSpaceGeometryMapping(self.spaceID, None, resPath) 问下这个resPath加载的文件在哪里,后端愣是没找到,前端倒是看到了,还是 ...
- [jquery] 遍历select的option,然后设置一项为选中
<script> var v={$menu.pid}; $("#pid option").each(function(){ if($(this).val()==v){ ...
- 计蒜客 A2232.程序设计:蒜厂年会-单调队列(双端队列(STL deque)实现)滑窗维护最小前缀和
程序设计:蒜厂年会 问答问题反馈 只看题面 16.79% 1000ms 262144K 在蒜厂年会上有一个抽奖,在一个环形的桌子上,有 nn 个纸团,每个纸团上写一个数字,表示你可以获得多少蒜币. ...
- (1)go 环境搭建
1 .下载安装 https://golang.org/ 2.环境变量配置 安装后会自动配置三个环境变量 (1) GOROOT: (2) PATH: (3) GOPATH GOPATH 从1.8开始,w ...
- WPS设置去广告
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha WPS设置去广告 设置密码和权限
- 初见Python<1>:基础语法
1.两个整数相除,计算结果的小数部分被截除,结果仍然是一个整数: 如:1/2=0 2.整数和浮点数相除.或者浮点数之间相除,结果有小数部分,仍然是一个浮点数: 如:1/2.0=0.5 1.0/2=0 ...
- SNOI2017(BZOJ5015~5018)泛做
T1:礼物 想错方向了,实际上很简单. 我想的是:显然题目求的是$\sum_{i=1}^{n} i^{k}2^{i}$,然后或许可以通过化式子变成与n无关的复杂度? 然后就不停往斯特林数反演和下降幂的 ...
- [USACO 2018 Feb Gold] Tutorial
Link: USACO 2018 Feb Gold 传送门 A: $dp[i][j][k]$表示前$i$个中有$j$个0且末位为$k$的最优解 状态数$O(n^3)$ #include <bit ...