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. vue模糊搜索&select取值

    之前vue1.0的过滤器真的很好使,但是作者为了不让搬运工变得太菜.硬是砍去了过滤器,为此,我还哭了好一阵,终于,一点一点的弄明白了过滤器是怎么回事后,也学明白了vue里的属性监听器computed以 ...

  2. pc、移动端H5网站 QQ在线客服、群链接代码【我和qq客服的那些事儿】

    转载:http://blog.csdn.net/fungleo/article/details/51835368#comments 移动端H5 QQ在线客服链接代码 <a href=" ...

  3. 【笔试题】精选30道Java笔试题解答

    转自于:精选30道Java笔试题解答 精选30道Java笔试题解答 1. 下面哪些是Thread类的方法() A. start() B. run() C. exit() D. getPriority( ...

  4. eclipse 查看jdk源码

    eclipse中引入jdk源码的设置:  设置: 1.点 "window"-> "Preferences" -> "Java" ...

  5. 启动Ubuntu时出现 /dev/sda2 clean 和 /dev/sda2 recovering journal 现象的解决办法

    最近在Ubuntu 18.4上安装Nvidia显卡后,显卡似乎总是不能完全兼容. 第一次出现问题时,是登录账号后,发现系统采用了默认显卡驱动,而已装过的显卡驱动则有损坏导致无法使用. 第二次出现问题时 ...

  6. HZAU 1203 One Stroke(倍增)

    题目链接:http://acm.hzau.edu.cn/problem.php?id=1203 [题意]给你一颗完全二叉树每个节点都有一个权值,然后要你从上往下找一条链,值得链上权值的和<K,且 ...

  7. javascrip异步问题

    for ( var i = 1; i <= 3; i++) { setTimeout( function (){ console.log(i); }, 0); };     一般人会以为输出结果 ...

  8. Luogu P3960 列队 线段树

    题面 线段树入门题. 我们考虑线段树来维护这个矩阵. 首先我们先定n+1棵线段树前n棵维护每行前m-1个同学中没有离队过的同学,还有一棵维护第m列中没有离队过的同学.再定n+1棵线段树前n棵线段树维护 ...

  9. 【线段树】Petrozavodsk Summer Training Camp 2016 Day 6: Warsaw U Contest, XVI Open Cup Onsite, Sunday, August 28, 2016 Problem H. Hay

    有一些草,一开始高度都是0,它们的生长速率不同. 给你一些单增的日期,在这些日期要将>b的草的部分都割掉,问你每次割掉的部分有多少. 将草的生长速率从大到小排序,这样每次割掉的是一个后缀,而且不 ...

  10. 【二分图】【并查集】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem L. Canonical duel

    给你一个网格(n<=2000,m<=2000),有一些炸弹,你可以选择一个空的位置,再放一个炸弹并将其引爆,一个炸弹爆炸后,其所在行和列的所有炸弹都会爆炸,连锁反应. 问你所能引爆的最多炸 ...