[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 ...
随机推荐
- 【bzoj1132】[POI2008]Tro 计算几何
题目描述 平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和 N<=3000 输入 第一行给出数字N,N在[3,3000] 下面N行给出N个点的坐标,其值在[0,10000] 输出 保留 ...
- 转:VMware攻击界面分析
转:https://comsecuris.com/blog/posts/vmware_vgpu_shader_vulnerabilities/ Wandering through the Shady ...
- SecureFXPortable中文乱码
SecureFXPortable有一个非常奇怪的地方,明明在全局选项中已经将编码设置为UTF-8,但连接Linux还是会出现中文乱码. 后来发现这个全局选项竟然不对SecureFXPortable生效 ...
- JNDI Tomcat
1.JNDI的诞生及简介简介 1)服务器数据源配置的诞生 JDBC阶段: 一开始是使用JDBC来连接操作数据库的: 在Java开发中,使用JDBC操作数据库的四个步骤如下: ①加载数据库驱动程序(Cl ...
- shell 读配置文件
今天跟同事探讨了一下 shell 脚本中对配置文件的读写问题.在此总结一下常用的配置文件的读写方式.大多数的配置文件都是以key=value形式存在的.配置项完全由键值对组成.这样的配置文件读写也是最 ...
- 【20181024T1】小C的数组【二分+dp】
题面 [正解] 题目求最大的最小,可以二分 设\(f_i\)表示第i个数不改满足条件需要改多少个 可以从j转移,那么[j+1,i]的均匀摊开后的差值应该在范围内 容易推出方程: \(f_i=min_{ ...
- 【区间dp】【记忆化搜索】UVALive - 3516 - Exploring Pyramids
f(i,j)=sum(f(i+1,k-1)*f(k,j) | i+2<=k<=j,Si=Sk=Sj). f(i+1,k-1)是划分出第一颗子树,f(k,j)是划分出剩下的子树. #incl ...
- Problem F: 零起点学算法101——统计字母数字等个数
#include<stdio.h> #include<string.h> int main(){ ]; while(gets(str)!=NULL){ ,b=,c=,d=; ; ...
- [转]Android的ADT与SDK的区别
adt只是一个eclipse的插件,里面可以设置sdk路径 SDK(Software Development Kit): 一般是一些被软件工程师用于为特定的软件包.软件框架.硬件平台.操作 ...
- httpWebRequest 文件下载
服务版本: go file system ssdb github: https://github.com/dtxlink/gfs 上一篇: 一个 go 文件服务器 ssdb 通过 httpWebReq ...