Redux / Redux Saga - 如何等待商店更新?

use*_*633 5 reactjs redux redux-saga react-redux

我正在将 React 与 Redux 和 Sagas 结合使用来创建纸牌游戏模拟器。我需要创建一个函数来更新 redux 状态,等待新状态并再次运行。我不确定在 React Redux 领域是否/如何实现这样的事情。以下是我想要实现的简化版本:

function endTurn() {
  // do something
}

function playerTurn(playerHand) {
  const decision = getPlayerDecision(playerHand);
  updatePlayerHand(decision); // this dispatches an action to update redux state

  // If player chose to 'Stand', the game ends.
  // Otherwise, it's player's turn again.
  if(decision === 'Stand') {
    endTurn();
  } else {
    // here I need the updated hand, how do I get it?
    playerTurn(updatedHand);
  }
}
Run Code Online (Sandbox Code Playgroud)

一个明显的解决方案是将此逻辑放在 'componentWillReceiveProps' 中,但它似乎不正确,而且我确信它最终会出现很多问题。直觉上,感觉像是 Redux Saga 的一份工作,但我在文档中找不到任何相关内容。有什么建议?

解决方案: Krasimir 使用 的回答为yield select我指明了正确的方向。下面是我最终得到的代码的简化版本:

import { put, takeEvery, select } from 'redux-saga/effects';

function* playPlayerTurn() {

  const playerHand = yield select(getPlayerHand);
  const decision = getPlayerDecision(playerHand);

  // some action that executes the decision
  // the action results in a change to playerHand
  yield put({
    type: `PLAY_PLAYER_DECISION`,
    decision,
  });

  // If player chose to 'Stand', the turn ends.
  // Otherwise, it's player's turn again.
  if(decision === 'Stand') {
    console.log('PLAYER OVER');
  } else {
    console.log('PLAYER AGAIN');
    yield put({
      type: `PLAY_PLAYER_TURN`,
    });
  }
}

export function* playerTurnWatcher() {
  yield takeEvery(`PLAY_PLAYER_TURN`, playPlayerTurn);
}
Run Code Online (Sandbox Code Playgroud)

本质上,我可以递归地调用传奇

Kra*_*mir 5

假设您分派了一个PLAYER_HAND有效载荷为decision.

import { select, takeLatest } from 'redux-saga/effects';

const waitForPlayerTurn = function * () {
  takeLatest(PLAYER_HAND, function * (decision) {
    // at this point the store contains the right `decision`
    if(decision === 'Stand') {
      endTurn();
    } else {
      playerTurn(yield select(getUpdateHand)); // <-- getUpdateHand is a selector
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

当然,您必须运行waitForPlayerTurn传奇。