With our Redux implementation lousy with State ADT based reducers, it is time to hook it all up to a React Shell. Having already built out some UI/UX in React that is connected to our store, we’ll spend the first part of this lesson with a quick tour of how our store integrates using the standard react-redux library.

Once we get a handle on our state's propagation through the app, we can focus in on how we will dispatch our actions during game play. We’ll implement the ability to start the game by using the startGame action creator to create a dispatching function that is passed through our component tree, down to the Start Game button in our Playing Area.

Add redux dev tool to the appliation:

import { createStore, compose } from 'redux'

import identity from 'crocks/combinators/identity'

import { initialState } from './model/initialize'

import reducer from './reducers'

const composeEnhancers =
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose export default createStore(
reducer,
initialState(),
composeEnhancers(identity) // add devtool
)

Provide Store for React application:

index.js:

import React from 'react'
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux' import './index.css' import store from './data/store'
import Game from './Game' ReactDOM.render(
<Provider store={store}>
<Game />
</Provider>,
document.getElementById('root')
)

Dispatch action from component:

import React from "react";
import PropTypes from "prop-types"; import pick from "crocks/helpers/pick";
import unit from "crocks/helpers/unit"; import { connect } from "react-redux";
import { startGame } from "./data/reducers/game"; import "./Game.css"; import Bunny from "./components/Bunny";
import Feedback from "./components/Feedback";
import Messages from "./components/Messages";
import PlayArea from "./components/PlayArea";
import GameOver from "./components/GameOver"; const Game = props => {
const {
answer,
cards,
hasWon,
hint,
isCorrect,
moves,
start, // passed in from here
rank,
restart
} = props; return (
<div className="game">
<Bunny rank={rank} />
<PlayArea answer={answer} cards={cards} startGame={start} /> <!-- Used here -->
<Messages hint={hint} moves={moves} />
<Feedback isCorrect={isCorrect} />
<GameOver hasWon={hasWon} restartGame={restart} />
</div>
);
}; Game.propTypes = {
answer: PropTypes.func.isRequired,
cards: PropTypes.array.isRequired,
isCorrect: PropTypes.bool,
hasWon: PropTypes.bool,
hint: PropTypes.object.isRequired,
moves: PropTypes.number.isRequired,
start: PropTypes.func.isRequired,
rank: PropTypes.number.isRequired,
restart: PropTypes.func.isRequired
}; const mapState = pick([
"cards",
"hasWon",
"hint",
"isCorrect",
"moves",
"rank"
]); const mapDispatch = dispatch => ({
answer: unit,
restart: unit,
start: () => dispatch(startGame()) // Connect to our State ADT
}); export default connect(
mapState,
mapDispatch
)(Game);

[React + Functional Programming ADT] Connect State ADT Based Redux Actions to a React Application的更多相关文章

  1. [Functional Programming] Combine Multiple State ADT Instances with the Same Input (converge(liftA2(constant)))

    When combining multiple State ADT instances that depend on the same input, using chain can become qu ...

  2. [Functional Programming] Compose Simple State ADT Transitions into One Complex Transaction

    State is a lazy datatype and as such we can combine many simple transitions into one very complex on ...

  3. [Functional Programming ADT] Create State ADT Based Reducers (applyTo, Maybe)

    The typical Redux Reducer is function that takes in the previous state and an action and uses a swit ...

  4. [React + Functional Programming ADT] Create Redux Middleware to Dispatch Actions with the Async ADT

    We would like the ability to group a series of actions to be dispatched with single dispatching func ...

  5. [React + Functional Programming ADT] Create Redux Middleware to Dispatch Multiple Actions

    We only have a few dispatching functions that need to be known by our React Application. Each one ac ...

  6. [Functional Programming] Define Discrete State Transitions using the State ADT

    We build our first state transactions as two discrete transactions, each working on a specific porti ...

  7. [Functional Programming] Reader with Async ADT

    ReaderT is a Monad Transformer that wraps a given Monad with a Reader. This allows the interface of ...

  8. [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 ...

  9. [Functional Programming Monad] Substitute State Using Functions With A State Monad (get, evalWith)

    We take a closer look at the get construction helper and see how we can use it to lift a function th ...

随机推荐

  1. 02 java 基础:java 文件名与类名关系 CLASSPATH

    java 类修饰符:通常情况下使用 public 修饰,此时,java 强制要求 .java 文件名需与该 public 修饰类名一致,否则无法编译通过.如若没有加修饰符,文件名与类名可无任何关联. ...

  2. 【剑指offer】面试题 55. 二叉树的深度

    面试题 55. 二叉树的深度 题目一:二叉树的深度 题目描述:输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. Java 实现 ...

  3. ps aux 状态介绍

    ps aux 输出 参数 含义 详解 运行 ps aux 的到如下信息:   ps auxUSER    PID   %CPU %MEM VSZ   RSS TTY    STAT   START T ...

  4. 《深入理解Android2》读书笔记(五)

    接上篇<深入理解Android2>读书笔记(四) startActivity Am void run() throws RemoteException { try { printMessa ...

  5. Luogu P2486 染色(树链剖分+线段树)

    题解 不妨采取重链剖分的方式把路径剖成区间,然后用线段树维护,考虑如何合并一个区间 struct Node { int lf, rg, tot; }seg[N << 2]; int col ...

  6. 解决CDH的web界面使用nginx代理一些静态文件无法加载

    vim /opt/cm-/share/cmf/webapp/WEB-INF/spring/mvc-config.xml .... 注释此行 <bean class="com.cloud ...

  7. Flask实战第40天:图片验证码生成技术

    图片验证码生成 安装pillow pip install pillow 在utils下新建python package命名为captcha 把需要需要用到的字体放在captcha下 编辑captcha ...

  8. python 去掉换行符或者改为其他方式结尾的方法(end='')

    输入参数不换行,就是打印之后不换行,在python2.7中使用 "," >>>def test(): print 'hello', print "wor ...

  9. bzoj 2137: submultiple

                                                     Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 23 ...

  10. 【bzoj1594】猜数游戏

    1594: [Usaco2008 Jan]猜数游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 556  Solved: 225 Descripti ...