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);

最新文章

  1. linux上使用google身份验证器(简版)
  2. js通过继承实现私有函数
  3. C#变量详解
  4. Linux 下自解压文件的制作
  5. 通过Mac远程调试iPhone/iPad上的网页(转)
  6. Ubuntu基础命令
  7. open_binary_frm
  8. python手记(38)
  9. Bash&#39;s Big Day
  10. while循环与 for循环
  11. Spring Mvc和Spring Boot配置Tomcat支持Https
  12. CUDA各版本官方下载地址
  13. 【Hive学习之一】Hive简介
  14. Linux - awk 文本处理工具二
  15. [Android Pro] 开发一流的 Android SDK:Fabric SDK 的创建经验
  16. iOS try catch
  17. UVa 10285 Longest Run on a Snowboard - 记忆化搜索
  18. 电脑连接树莓派Pi Zero W
  19. 批量删除Redis中的数据
  20. [转]边框回归(Bounding Box Regression)详解

热门文章

  1. win10的VMware虚机host-only模式下,虚拟机无法ping通物理机,而物理机能ping通虚机
  2. bzoj 1407 扩展欧几里德
  3. 五十三 网络编程 TCP/IP简介
  4. Mongo Connector for BI
  5. LOJ #6284. 数列分块入门 8-分块(区间查询等于一个数c的元素,并将这个区间的所有元素改为c)
  6. Codeforces 1131 F. Asya And Kittens-双向链表(模拟或者STL list)+并查集(或者STL list的splice()函数)-对不起,我太菜了。。。 (Codeforces Round #541 (Div. 2))
  7. Sd - 数据库事务
  8. canvas元素内容生成图片
  9. 2017 Multi-University Training 2 解题报告
  10. AtCoder - 1999 Candy Piles