[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 ...
随机推荐
- [水煮 ASP.NET Web API2 方法论](1-5)ASP.NET Web API Scaffolding(模板)
问题 我们想快速启动一个 ASP.NET Web API 解决方案. 解决方案 APS.NET 模板一开始就支持 ASP.NET Web API.使用模板往我们的项目中添加 Controller,在我 ...
- python 截取指定长度汉字
这个方法不是很好,不知道有没有更好的方法 def cut_hz(s, length): charstyle = chardet.detect(s) t = s[:length] try: unicod ...
- Mac OS X 更新JAMF域控配置
在终端执行以下命令即可更新jamf域控配置属性 sudo jamf mcx # 应用被管理的配置信息 sudo jamf policy -trigger # 检查触发器策略 sudo jamf rec ...
- Envious Exponents
问题 E: Envious Exponents 时间限制: 1 Sec 内存限制: 128 MB提交: 321 解决: 53[提交] [状态] [讨论版] [命题人:] 题目描述 Alice an ...
- 【DFS】Anniversary Cake
[poj1020]Anniversary Cake Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17203 Accep ...
- BZOJ 1115 [POI2009]石子游戏Kam(阶梯博弈)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1115 [题目大意] 有N堆石子,除了第一堆外,每堆石子个数都不少于前一堆的石子个数. ...
- 【最短路】【spfa】CDOJ1633 去年春恨却来时,落花人独立,微雨燕双飞
对于S集合中的数,例如a1,考虑到如果x能够被表示出来,那么x+a1也一定能被表示出来 设d[r]为所有模a1余r的数中,能被表示出来的最小的数 用d[x]+ai去更新d[(x+ai)%a1],跑最短 ...
- KVM使用virsh console无法连接的解决办法(转)
一.问题描述: KVM中宿主机通过console无法连接客户机,卡在这里不动. # virsh console vm01 Connected to domain vm01 Escape charact ...
- Linux下Apache、PHP、MySQL默认安装路径
Apache: 如果采用RPM包安装,安装路径应在 /etc/httpd 目录下 Apache配置文件:/etc/httpd/conf/httpd.conf Apache模块路径:/usr/sbin/ ...
- 如何释放 DB_RECOVERY_FILE_DEST_SIZE
转自原文 如何釋放 DB_RECOVERY_FILE_DEST_SIZE,有删减 oracle默認安裝之後,如何沒有手動設置歸檔路徑(alter system set log_archive_dest ...