[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 ...
随机推荐
- nodejs里的express自动刷新gulp-express使用【转载】
搬运自[http://blog.csdn.net/zhu_free/article/details/51476525] gulp-express实现实时刷新 本来使用gulp-connect可以创建本 ...
- 阿里云Maven仓库配置,Maven镜像配置
Jenkins通过maven对java代码打包编译时,速度太慢,所以修改为阿里的Maven仓库 修改如下: [root@7mini-node2 conf]# vim /software/apache- ...
- JS获取网页高度和宽度
注:此文属于转载自他人博客 网页可见区域宽: document.body.clientWidth 网页可见区域高: document.body.clientHeight 网页可见区域宽: docume ...
- 选择排序(SelectionSort)
http://blog.csdn.net/magicharvey/article/details/10274765 算法描述 选择排序是一种不稳定排序.选择排序每次交换一对元素,它们当中至少有一个将被 ...
- oracle10g中判断字段是否为空的坑
RT,在oracle中,写SQL时,假设这个字段为STA Char(3),判断这个字段是否为空一般都是这两个:STA = '' or STA is null 但是今天这两种方法失效了,无论是STA = ...
- 可持久化01Trie树+LCA【p4592】[TJOI2018]异或
Description 现在有一颗以\(1\)为根节点的由\(n\)个节点组成的树,树上每个节点上都有一个权值\(v_i\).现在有\(Q\)次操作,操作如下: 1\(\;x\;y\):查询节点\(x ...
- 安装zabbix监控系统
环境 操作系统 最小化安装CentOS Linux release 7.2.1511 IP 192.168.88.1 zabbix版本 zabbix-3.4.4.tar.gz zabbix依赖于LNM ...
- Html导出Pdf
最近领导给了一个新的需求:给了我一个html页面,让我导出Pdf页面 由于以前没有搞过这方面的需求,所以查百度找资料,找了一大堆的好东西,像ItextSharp和wkhtmltopdf 废话不说,进入 ...
- PM过程的一些典型场景和问题
如何进行团队激励 如何进行目标管理 如何进行绩效考核 如何处理团队沟通(技巧) 详述几种软件过程理论 需求分析和度量 测试过程和工具 开发管理过程
- 【jzyzoj】【p1320 patrol】 巡逻(网络流最小割例题)
描述 Description FJ有个农场,其中有n块土地,由m条边连起来.FJ的养牛场在土地1,在土地n有个新开张的雪糕店.Bessie经常偷偷溜到雪糕店,当Bessie去的时候,FJ就要跟上她.但 ...