1

I can't see a clear way to know when a particular action has fired or particular state has updated from the context of a redux-thunk action creator.

I want to do something like this:

  1. Dispatch an action
  2. Detect a possible recoverable error condition
  3. Error condition dispatches a different action signalling recovery process initiating
  4. Wait for recovery to complete
  5. Proceed with current action or re-dispatch it

Concrete example:

  1. User interaction triggers API call action
  2. Note that API call failed, needs login
  3. Dispatch 'LOGIN_REQUIRED' action, which pops up a <dialog> for user.
  4. Wait for logged in state to change (or LOGIN_SUCCESS action to occur, whatever).
  5. Make same API call again
4

2 回答 2

1

如果要检查要调度的特定操作,则需要中间件。

实际上,如果您想“订阅给定的状态”,Redux 并没有提供内置的方法来做到这一点。但是,有许多实用程序可以为您实现这种逻辑。请参阅http://redux.js.org/docs/FAQ.html#store-setup-subscriptions上的 Redux FAQ ,以及我的 Redux 插件目录中的商店订阅插件列表。Redux 中间件列表也可能对“监听操作”场景有用。)

于 2016-05-19T20:21:40.460 回答
0

为了可视化商店中发生的事情,您可以使用:

import {composeWithDevTools} from 'redux-devtools-extension';

并将您的商店创建为:(例如使用 thunk 中间件)

const store = `createStore(rootReducer,composeWithDevTools(applyMiddleware(thunk)));`

然后你可以在浏览器中使用扩展。你可以通过{connect} from 'react-redux'as props 访问你的状态。然后您的操作将通过减速器更改状态,这将导致您的组件接收新的道具,因此您可以将逻辑放入

componentWillReceiveProps(nextProps) {/*your business logic*/}
于 2016-12-13T20:03:35.193 回答