[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 ...
随机推荐
- HBase shell 命令创建表及添加数据操作
创建表,表名hbase_1102,HBase表是由Key-Value组成的,此表中Key为NAME 此表有两个列族,CF1和CF2,其中CF1和CF2下分别有两个列name和gender,Chin ...
- 使用php后台给自己做一个页面路由,配合ajax实现局部刷新。
今天就要放假了,把近来囤积的小玩意儿总结整理一下. 在请求一个html页面来嵌入到当前页会有一个问题,就是跟随请求过来的html他的样式表和脚本会失效.是因为文档加载的先后顺序等问题造成的.因此,加载 ...
- Beaglebone Black教程BeagleBone Black安装最新系统映像
Beaglebone Black教程BeagleBone Black安装最新系统映像 BeagleBone Black安装最新系统映像 Beaglebone Black虽然已经预装了Debian操作系 ...
- python 写文件write(string), writelines(list) ,读文件
read()方法用于直接读取字节到字符串中,可以接参数给定最多读取的字节数,如果没有给定,则文件读取到末尾. readline()方法读取打开文件的一行(读取下个行结束符之前的所有字节),然后整行,包 ...
- hdu 1864 最大报销额(背包)
最大报销额 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- BZOJ 2157 旅游(动态树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2157 [题目大意] 支持修改边,链上查询最大值最小值总和,以及链上求相反数 [题解] ...
- BZOJ 3524 [Poi2014]Couriers(二分+蒙特卡罗)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3524 [题目大意] 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个 ...
- 【Matrix-tree定理】【并查集】【kruscal算法】bzoj1016 [JSOI2008]最小生成树计数
题意:求一个图的最小生成树个数. 矩阵树定理:一张无向图的生成树个数 = (度数矩阵 - 邻接矩阵)的任意一个n-1主子式的值. 度数矩阵除了对角线上D[i][i]为i的度数(不计自环)外,其他位置是 ...
- 【序列莫队+树状数组】BZOJ3289-Mato的文件管理
[题目大意] 一共有n份,每份有一个大小和一个编号.Mato每天随机选一个区间[l,r],拷贝出来(即对原序列不影响),给它们排序,并且每次只能交换相邻两份文件.问每天最少交换几次? [思路] 显然, ...
- 20162312 实验一 Linux基础与Java开发环境
实 验 报 告 课程:程序设计与数据结构 姓名:张家铖 班级:1623 学号:20162312 实验名称: Linux基础与Java开发环境 实验器材:msi GL62M 7RD 实验目的与要求:1. ...