博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[React + Functional Programming ADT] Connect State ADT Based Redux Actions to a React Application
阅读量:5113 次
发布时间:2019-06-13

本文共 2767 字,大约阅读时间需要 9 分钟。

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__ || composeexport 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(  
, 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 (    
);};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);

 

 

转载于:https://www.cnblogs.com/Answer1215/p/10363023.html

你可能感兴趣的文章
2009年度计划
查看>>
css雪碧技术
查看>>
extjs TabPanel 监听激活事件
查看>>
命运总是喜欢开玩笑
查看>>
为什么到今天还要坚持写博客
查看>>
Hibernate报错 ** is not mapping
查看>>
网络运维基础知识手册
查看>>
第16周进度表
查看>>
"fcitx按ctrl+space没反应"解决方法
查看>>
java连接mysql数据库
查看>>
深入理解JavaScript中的this关键字
查看>>
上传文件带进度条
查看>>
easylui datagrid 动态生成列
查看>>
alpha-咸鱼冲刺day8
查看>>
[BZOJ 1033] [ZJOI2008] 杀蚂蚁antbuster 【模拟!】
查看>>
【JS笔记】5.3 Date类型
查看>>
格式话输出
查看>>
微信小程序访问后台出现 对应的服务器证书无效。控制台输入 showRequestInfo() 可以获取更详细信息。...
查看>>
免费 SVN 服务器收集
查看>>
页面跳转
查看>>