redux-saga 一直调用action

「已注销」 2020-12-06 05:41:51
saga.js

import {takeEvery, put} from 'redux-saga/effects'
import {GET_NAME_VALUE} from './actionTypes'
import axios from 'axios'
import {getName} from './actionCreators'



function* fetchGetName(action) {
console.log(action)
const res = yield axios.get('/test.json')
const name = res.data.name
yield put(getName(name))
}


function* mySaga() {
yield takeEvery(GET_NAME_VALUE, fetchGetName)
}

export default mySaga



组件

import React, { Component } from 'react'
import store from './store'
import { getName } from './store/actionCreators'
export default class App extends Component {
constructor(props) {
super(props)

this.state = store.getState()
}

render() {
return (
<div>
<button onClick={() => this.handleGetName()}>SHOW NAME</button>
<p>{this.state.name}</p>
</div>
)
}

// 监听store 重新更改页面中的state
componentDidMount() {
store.subscribe(() => this.setState(store.getState()))
}

handleGetName() {
store.dispatch(getName())
}
}



可以在控制台中或者redux tools中看到一直在调用这个action
请问这是为什么?
...全文
22 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧