我是redux-sagas的新手,我仍然试图了解他们的工作方式。我试图将一个redux-saga函数绑定到我的一个组件上,然后发出一个API请求,然后用响应中的数据调度一个动作。在根据文档尝试将调度传递给我的传奇函数参数时,我收到了调度错误的定义。这是我的设置:
缩略图容器
import React, { PureComponent } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { getThumbnails } from '../../../containers/search/sagas';
import createReducer from './reducers';
class ThumbnailsContainer extends PureComponent {
constructor(props) {
super(props);
}
componentWillMount() {
this.props.getThumbnails()
}
render() {
return (
<div>My Container</div>
)
}
}
function mapDispatchToProps(dispatch) {
return {
getThumbnails: bindActionCreators(getThumbnails, dispatch)
}
}
export default connect(null, mapDispatchToProps)(ThumbnailsContainer);
Sagas.js
export function* getThumbnails(dispatch) {
console.log(dispatch)
yield fetch('myAPIurl', {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: {}
}).then(res=>res.json())
.then(res => {
console.log(JSON.parse(res.data))
});
}
rootsagas.js
import { all, fork } from 'redux-saga/effects';
export default function* rootSaga() {
yield all([
fork(getThumbnails)
]);
}
Stores.js
import { applyMiddleware, compose, createStore } from 'redux';
import createSagaMiddleware from 'redux-saga';
import rootSaga from './rootsagas';
const sagaMiddleware = createSagaMiddleware();
const apolloMiddleware = apolloClient.middleware();
export default function configureStore(initialState = {}, history) {
const middlewares = [
sagaMiddleware
];
})
store.runSaga = sagaMiddleware.run(rootSaga);
store.asyncReducers = {}; // Async reducer registry
const store = createStore(createReducer(), applyMiddleware(...middlewares));
store.runSaga = sagaMiddleware.run(rootSaga);
store.asyncReducers = {}; // Async reducer registry
return store;
我想做的是在我获得API响应后发出一个动作,但似乎无法掌握调度功能。无法在他们的文档中看到我哪里出错了?
答案 0 :(得分:0)
不确定是否正确。尝试使用put
中的redux-saga
。如下所示
import { put } from 'redux-saga/effects';
function* yourGenerator() {
/*Your code*/
yield put({ type: 'YOUR_ACTION', YOUR_PAYLOAD });
}
使用saga here了解有关调度操作的详情。