为什么在yield put(action)会导致网页无限加载
来源:6-9 Redux-saga中间件入门(2)
慕码人7077953
2020-12-02
RT
我最后改成了return put(action)就好了,这是为啥呢?
app.js中componentDidMount:
componentDidMount(){
store.subscribe(this.handlesubscription.bind(this));
let mock = new MockAdapter(axios);
mock.onGet('/todos').reply(200,{
data:'this is mockup'
});
const action = getTodos();
store.dispatch(action);
}
handlesubscription = prevState=>{
this.setState(store.getState())
}
actionCreators.js中的action
import { createAction } from '@reduxjs/toolkit';
import {GET_TODOS} from './actionlib'
export const getTodos = createAction(GET_TODOS)
sagas.js代码
import {takeEvery, put} from 'redux-saga/effects'
import {UPDATE_ITEM_INPUT, SUBMIT_ITEM_INPUT, DELETE_ITEM_DATA, GET_TODOS} from './actionlib'
import {getTodos} from './actionCreators'
import axios from 'axios';
function* getTodos_saga(){
const res = yield axios.get('/todos');
const action = yield getTodos(res.data.data);
return put(action);
}
function* mySaga() {
yield takeEvery(GET_TODOS, getTodos_saga);
}
export default mySaga;
写回答
2回答
-
Dell
2020-12-04
你这代码明显就是个死循环啊:
function* getTodos_saga(){
const res = yield axios.get('/todos');
const action = yield getTodos(res.data.data);
return put(action);
}022021-05-19 -
Dell
2020-12-03
代码贴上来看看
012020-12-03
相似问题