[React + Functional Programming ADT] Connect State ADT Based Redux Actions to a React Application

本文详细介绍了如何将Redux状态管理库与React应用进行整合。首先,通过使用react-redux库,我们实现了Redux store与React组件之间的状态传递。接着,演示了如何在游戏组件中通过dispatch函数触发startGame动作创建者,实现游戏开始的逻辑。最后,展示了如何在项目中引入并配置Redux DevTools,以便于开发者更好地理解和调试状态变化。

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

 

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值