[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 ...
随机推荐
- JS获取网页高度和宽度
注:此文属于转载自他人博客 网页可见区域宽: document.body.clientWidth 网页可见区域高: document.body.clientHeight 网页可见区域宽: docume ...
- sql 触发器 游标
在数据库中,删除一条记录的同时想要删除另一个表里的数据,这时我们可以选择使用触发器.触发器主要是通过事件进行触发被自动调用执行的,而存储过程可以通过存储过程的名称被调用.触发器是当对某一个表进行操作. ...
- [putty] ubuntu 通过配置文件设置字体
创建了一个session之后,就能在 ~/.putty/sessions/ 文件夹下看到session的配置文件了 $ vim ~/.putty/sessions/session-name 搜索Fon ...
- 洛谷P1196 [NOI2002] 银河英雄传说
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #includ ...
- 爬楼梯(LintCode)
爬楼梯 假设你正在爬楼梯,需要n步你才能到达顶部.但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部? 样例 比如n=3,中不同的方法 返回 3 用递归又超时了..于是又换了DP,dp并不熟 ...
- Loj#6432「PKUSC2018」真实排名(二分查找+组合数)
题面 Loj 题解 普通的暴力是直接枚举改或者不改,最后在判断最后对哪些点有贡献. 而这种方法是很难优化的.所以考虑在排序之后线性处理.首先先假设没有重复的元素 struct Node { int p ...
- 树形dp入门(poj 2342 Anniversary party)
题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚 ...
- hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)
Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K ...
- HDU 6035 Colorful Tree (树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6035 [题目大意] 给出一颗树,一条路径的价值为其上点权的种类数,求路径总价值 [题解] 我们计算 ...
- kong添加upstream
整理的文档比较早,kong版本可能是0.10.3版本.详情请看官网最新文档 准备 使用kong代理后端请求 1.开放几个接口如下: 本地请求1:http://aaa.wyc.com:8888/aaa ...