为什么在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);
}

0
2
慕侠6607443
const action = yield getTodos(res.data.data); 是因为执行这一句 所以成了死循环吗
2021-05-19
共2条回复

Dell

2020-12-03

代码贴上来看看

0
1
慕码人7077953
修改上去了哈,您看下
2020-12-03
共1条回复

React零基础入门到实战,完成企业级项目简书网站开发

主流新技术 React-redux,React-router4,贯穿基础语法

5275 学习 · 2496 问题

查看课程