[React + Functional Programming ADT] Connect State ADT Based Redux Actions to a React Application
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [Functional Programming] Reader with Async ADT
ReaderT is a Monad Transformer that wraps a given Monad with a Reader. This allows the interface of ...
- [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] 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 ...
随机推荐
- Lucene.Net无障碍学习和使用:索引篇
一.简单认识索引 Lucene.Net的应用相对比较简单.一段时间以来,我最多只是在项目中写点代码,利用一下它的类库而已,对很多名词术语不是很清晰,甚至理解 可能还有偏差.从我过去的博客你也可以看出, ...
- Sonar常见的审查结果
格式:问题名字+问题出现的次数 Resources should be closed2 资源未关闭,打开发现有两处用到的IO流没有关闭 Conditions should not unconditio ...
- No module named '_Sqlite3' 解决方法
今晚,在学习Python的时候,(学习链接:http://yidao620c.github.io/blog/20150420/simpleblog-01.html(搭载自己的博客案例)): 想为系统生 ...
- JavaScript中思考do...while 和 while语句的区别
Do...while和while...do的区别在于对于临界值的测试上.当在执行的时候只选择临界值来测试时,你会发现do...while至少要执行一次,而while...do则一次都不会执行.但是,当 ...
- Python开发基础-Day23try异常处理、socket套接字基础1
异常处理 错误 程序里的错误一般分为两种: 1.语法错误,这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正 2.逻辑错误,人为造成的错误,如数据类型错误.调用方法错误等,这些解 ...
- ubuntu问题集锦
我使用的是ubuntu 14.04 用UltraIOS 制作镜像安装的 ubuntu 问题1:闪屏问题以及文字显示不全 解决方案:重装显卡驱动 解决过程:http://my.oschina.net/ ...
- [Lydsy1805月赛] quailty 算法
稍微建一下模型就可以发现,题目要求的其实是一个最小异或基环森林.... 可以用类似最小生成树的拟阵性质来证明,贪心的从小的边权开始依次尝试加入的方法是对的. 所以我们把a[]排完序之后直接递归贪心就行 ...
- 【Floyd】噪音恐惧症
[UVA10048]噪音恐惧症 题面略 试题分析:直接Floyd一下维护u到v的路径最大值最小就可以了,1A 代码: #include<iostream> #include<cstr ...
- 【贪心】hdu5969 最大的位或
对于右端点r和左端点l,考虑他们的二进制位从高到低,直到第一位不同的为止. 更高的都取成相同的,更低的都取成1. 比如 101011110001 101011101001 101011111111 # ...
- bzoj 3728: PA2014Final Zarowki
3728: PA2014Final Zarowki Description 有n个房间和n盏灯,你需要在每个房间里放入一盏灯.每盏灯都有一定功率,每间房间都需要不少于一定功率的灯泡才可以完全照亮.你可 ...