Saga 观察者未接收或处理已派发的操作
Saga watcher not receiving or processing dispatched action
我有一个 watcher saga,它不断地监听某种类型的动作。这是从 saga.run() 开始的。当它接收到某种类型的动作时,它应该调用另一个执行异步动作的传奇。然而,第二个传奇中的断点永远不会被调用。
所以要么原始代码没有正确调度操作,要么 watcher saga 配置错误。
守望者传奇代码
import { all, fork, takeLatest } from 'redux-saga/effects'
import {fetchItems} from './itemsSaga';
function* watchLoadItemsRequest() {
while(true) {
yield takeLatest('FETCH_ITEMS_REQUEST', fetchItems);
}
}
export default function* root() {
yield all ([
fork(watchLoadItemsRequest)
])
}
我的 itemsSaga(未被调用)
import { call, put } from 'redux-saga/effects'
import fetchItemsCompleted from '../actions/fetchItemsCompleted';
import httpCallCompleted from '../actions/httpCallCompleted';
import httpCallStarted from '../actions/httpCallStarted';
import itemAPI from '../api/itemAPI';
import ItemsEntity from '../api/itemEntity';
// worker Saga: will be fired on FETCH_ITEMS_REQUEST actions
export function* fetchItems(action:any) {
let items : ItemEntity[];
yield put(httpCallStarted());
trades = yield call(itemAPI.getAllItemsAsync)
trades = [new ItemEntity(), new ItemEntity()]
yield put(fetchItemsCompleted(items))
yield put(httpCallCompleted());
}
我的商店创建代码
import { applyMiddleware, createStore } from 'redux';
import createSagaMiddleware from 'redux-saga';
import reducers from '../reducers';
import watchersSaga from '../sagas/watchersSaga';
export default function getStore() {
const sagaMiddleware = createSagaMiddleware();
const store = createStore(
reducers, applyMiddleware(sagaMiddleware)
);
sagaMiddleware.run(watchersSaga);
return store;
}
最后应该发送 FETCH_ITEMS_REQUEST 动作的控件
import * as React from 'react';
import { connect, Dispatch } from 'react-redux';
import ItemEntity from './api/itemEntity';
import './App.css';
import IStoreState from './interfaces/IStoreState';
interface IContentProps {
groupingType : string,
items: ItemEntity[],
loadItems : () => void
}
class MyContent extends React.Component<IContentProps,{}> {
constructor(props: any) {
super(props);
}
public componentDidMount() {
this.props.loadItems();
}
public render() {
return (
<div className="Center-content">
<div className="Center-content White">
<br/><br/>Grouped by {this.props.groupingType}<br/><br/>{this.props.items.length} items loaded
</div>
</div>
);
}
}
function mapStateToProps(state: IStoreState) {
return {
groupingType: state.navigation.groupingType,
trades: state.item.items
};
}
const mapDispatchToProps = (dispatch: Dispatch) => {
return {
loadItems: () => dispatch({type:'FETCH_ITEMS_REQUEST'})
}
}
export default connect(mapStateToProps, mapDispatchToProps)(PnlContent);
为什么这不起作用?有没有一种方法可以判断 FETCH_ITEMS_REQUEST 是否实际生成并发送到 redux?这样至少我可以缩小问题发生的范围。
更新:我调整了我的一个 reducer 来听 FETCH_ITEMS_REQUEST 并且它拾取了事件,所以我的控件可以很好地调度动作。看起来 WatcherSaga 在收到 FETCH_ITEMS_REQUEST 操作时没有触发 ItemsSaga fetchItems() 方法,所以我认为问题出在我的 watchersSaga
第二次更新:
越来越觉得是线条的问题
while(true) {
yield takeLatest('FETCH_ITEMS_REQUEST', fetchItems);
}
这似乎没有响应收到的 FETCH_ITEMS_REQUEST 消息和调用 fetchItems
对于任何有类似问题的人,我最终通过将我的 watchers Saga 更改为以下内容来解决它
function* watchLoadTradesRequest() {
yield takeEvery('FETCH_TRADES_REQUEST', fetchTrades);
}
// Register all your watchers
export default function* watchersRootSaga() {
yield all ([
watchLoadTradesRequest()
])
}
我有一个 watcher saga,它不断地监听某种类型的动作。这是从 saga.run() 开始的。当它接收到某种类型的动作时,它应该调用另一个执行异步动作的传奇。然而,第二个传奇中的断点永远不会被调用。
所以要么原始代码没有正确调度操作,要么 watcher saga 配置错误。
守望者传奇代码
import { all, fork, takeLatest } from 'redux-saga/effects'
import {fetchItems} from './itemsSaga';
function* watchLoadItemsRequest() {
while(true) {
yield takeLatest('FETCH_ITEMS_REQUEST', fetchItems);
}
}
export default function* root() {
yield all ([
fork(watchLoadItemsRequest)
])
}
我的 itemsSaga(未被调用)
import { call, put } from 'redux-saga/effects'
import fetchItemsCompleted from '../actions/fetchItemsCompleted';
import httpCallCompleted from '../actions/httpCallCompleted';
import httpCallStarted from '../actions/httpCallStarted';
import itemAPI from '../api/itemAPI';
import ItemsEntity from '../api/itemEntity';
// worker Saga: will be fired on FETCH_ITEMS_REQUEST actions
export function* fetchItems(action:any) {
let items : ItemEntity[];
yield put(httpCallStarted());
trades = yield call(itemAPI.getAllItemsAsync)
trades = [new ItemEntity(), new ItemEntity()]
yield put(fetchItemsCompleted(items))
yield put(httpCallCompleted());
}
我的商店创建代码
import { applyMiddleware, createStore } from 'redux';
import createSagaMiddleware from 'redux-saga';
import reducers from '../reducers';
import watchersSaga from '../sagas/watchersSaga';
export default function getStore() {
const sagaMiddleware = createSagaMiddleware();
const store = createStore(
reducers, applyMiddleware(sagaMiddleware)
);
sagaMiddleware.run(watchersSaga);
return store;
}
最后应该发送 FETCH_ITEMS_REQUEST 动作的控件
import * as React from 'react';
import { connect, Dispatch } from 'react-redux';
import ItemEntity from './api/itemEntity';
import './App.css';
import IStoreState from './interfaces/IStoreState';
interface IContentProps {
groupingType : string,
items: ItemEntity[],
loadItems : () => void
}
class MyContent extends React.Component<IContentProps,{}> {
constructor(props: any) {
super(props);
}
public componentDidMount() {
this.props.loadItems();
}
public render() {
return (
<div className="Center-content">
<div className="Center-content White">
<br/><br/>Grouped by {this.props.groupingType}<br/><br/>{this.props.items.length} items loaded
</div>
</div>
);
}
}
function mapStateToProps(state: IStoreState) {
return {
groupingType: state.navigation.groupingType,
trades: state.item.items
};
}
const mapDispatchToProps = (dispatch: Dispatch) => {
return {
loadItems: () => dispatch({type:'FETCH_ITEMS_REQUEST'})
}
}
export default connect(mapStateToProps, mapDispatchToProps)(PnlContent);
为什么这不起作用?有没有一种方法可以判断 FETCH_ITEMS_REQUEST 是否实际生成并发送到 redux?这样至少我可以缩小问题发生的范围。
更新:我调整了我的一个 reducer 来听 FETCH_ITEMS_REQUEST 并且它拾取了事件,所以我的控件可以很好地调度动作。看起来 WatcherSaga 在收到 FETCH_ITEMS_REQUEST 操作时没有触发 ItemsSaga fetchItems() 方法,所以我认为问题出在我的 watchersSaga
第二次更新:
越来越觉得是线条的问题
while(true) {
yield takeLatest('FETCH_ITEMS_REQUEST', fetchItems);
}
这似乎没有响应收到的 FETCH_ITEMS_REQUEST 消息和调用 fetchItems
对于任何有类似问题的人,我最终通过将我的 watchers Saga 更改为以下内容来解决它
function* watchLoadTradesRequest() {
yield takeEvery('FETCH_TRADES_REQUEST', fetchTrades);
}
// Register all your watchers
export default function* watchersRootSaga() {
yield all ([
watchLoadTradesRequest()
])
}