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. 微软企业库5.0 学习之路——第六步、使用Validation模块进行服务器端数据验证

    前端时间花了1个多星期的时间写了使用jQuery.Validate进行客户端验证,但是那仅仅是客户端的验证,在开发项目的过程中,客户端的信息永远是不可信的,所以我们还需要在服务器端进行服务器端的验证已 ...

  2. .NET ORM SqlQuery

    查询 1.SqlQuery 用于执行原生SQL和存储过程 //转成list List<Student> list1 = db.SqlQuery<Student>("s ...

  3. BZOJ 1026 windy数【数位DP】

    1026: [SCOI2009]windy数 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 10142  Solved: 4712[Submit][St ...

  4. JQuery总结+实例

    JQuery是什么? Jquery是继prototype之后又一个优秀的Javascript库.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF 1.5+, Safari ...

  5. [Codeforces-div.1 24D] Broken robots

    [Codeforces-div.1 24D] Broken robots 试题分析 显然设\(f_{i,j}\)为到\((i,j)\)的期望步数,将转移表达式列出来. 首先自己跟自己的项消掉. 然后规 ...

  6. 【DFS】【枚举】Gym - 101246G - Revolutionary Roads

    给你一张有向图,问你将任意一条边变成双向后,所能得到的最大强连通分量的大小. 缩点之后,预处理can(i,j)表示i能到j. 之后枚举每一条边(u,v),再枚举其他所有点t,如果can(u,t) &a ...

  7. 【树状数组逆序对】USACO.2011JAN-Above the median

    [题意] 给出一串数字,问中位数大于等于X的连续子串有几个.(这里如果有偶数个数,定义为偏大的那一个而非中间取平均) [思路] 下面的数据规模也小于原题,所以要改成__int64才行.没找到测试数据, ...

  8. Android系统UI交互控件Action Bar初探

    过年期间,Google正式宣布取消Android系统中MENU键的使用,也就是基于Android 4.0系统的手机都应没有MENU这一固定按键.这无疑是个变革性的改动,在我眼中,这似乎把Android ...

  9. 浙南联合训练赛 D - Broken Clock

    You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM fo ...

  10. Codeforces Round #345 (Div. 1) A - Watchmen 容斥

    C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a dang ...