gpt4 book ai didi

javascript - 在 promise 中 react setState 导致无限循环

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:55 36 4
gpt4 key购买 nike

预计

fetchServices() 被调用时,api.getServices 被调用并且在 promise 中 this.setState 被调用以改变 fetchingServicesfalse。然后隐藏加载微调器动画。

结果

由于某种原因,应用程序陷入了无限循环:

enter image description here

在我的服务容器中

constructor(props) {
super(props);
this.state = {
services: props.state.servicesReducer.services,
fetchingServices: true,
addingService: false
}
this.fetchServices = this.fetchServices.bind(this);
}

返回()

return (
<div className='services-container'>
<ul className='services-list'>
<li>
<AddServiceContainer />
</li>
{ this.state.fetchingServices
? <div className="icon-spin5 animate-spin"></div>
: null }

{ this.fetchServices() }
</ul>
</div>
)

最后是 fetchServices()

fetchServices() {
console.log('fetchServices')
api.getServices(12345).then(res => {
console.log(' api.getServices res:', res)

this.setState({
fetchingServices: false
});
});
}

完整代码

import React, { Component } from 'react'
import { connect } from 'react-redux'

import { AddServiceContainer } from './AddServiceContainer'
import { ServiceCard } from '../../components'
import { getServices } from '../../actions'
import * as api from '../../services/api'

export class ServicesContainer extends Component {
constructor(props) {
super(props);
this.state = {
services: props.state.servicesReducer.services,
fetchingServices: true,
addingService: false
}
this.fetchServices = this.fetchServices.bind(this);
}

onFormSubmit(e, user) {
e.preventDefault();
this.props.searchUser(user)
}

fetchServices() {
console.log('fetchServices')
api.getServices(12345).then(res => {
console.log(' api.getServices res:', res)

this.setState({
fetchingServices: false
});
});
}

render() {
return (
<div className='services-container'>
<ul className='services-list'>
<li>
<AddServiceContainer />
</li>
{ this.state.fetchingServices
? <div className="icon-spin5 animate-spin"></div>
: null }

{ this.fetchServices() }
</ul>
</div>
)
}
}

const mapStateToProps = (state) => {
return {
state
}
}

const mapDispatchToProps = (dispatch) => {
return {
getServices: (services) => { dispatch(getServices(services)) }
}
}

const ServicesListContainer = ServicesContainer;
export default connect(mapStateToProps, mapDispatchToProps)(ServicesListContainer)

最佳答案

每当您执行 setState 时,都会再次调用 render 方法。现在这里的问题是您在 render 方法中调用 fetchServices() 方法。现在,每当调用 fetchServices() 时,它都会调用一个 api。当 api 的结果出现时,您正在使用 setState 设置状态,这会导致重新渲染(即再次调用您的渲染方法),从而再次调用 fetchServices() .这就是它进入无限循环的原因。

解决方案:您应该像这样在 componentWillMount/componentDidMount 方法中调用您的 fetchServices():

import React, { Component } from 'react'
import { connect } from 'react-redux'

import { AddServiceContainer } from './AddServiceContainer'
import { ServiceCard } from '../../components'
import { getServices } from '../../actions'
import * as api from '../../services/api'

export class ServicesContainer extends Component {
constructor(props) {
super(props);
this.state = {
services: props.state.servicesReducer.services,
fetchingServices: true,
addingService: false
}
this.fetchServices = this.fetchServices.bind(this);
}

componentWillMount(){
this.fetchServices();
}
onFormSubmit(e, user) {
e.preventDefault();
this.props.searchUser(user)
}

fetchServices() {
console.log('fetchServices')
api.getServices(12345).then(res => {
console.log(' api.getServices res:', res)

this.setState({
fetchingServices: false
});
});
}

render() {
return (
<div className='services-container'>
<ul className='services-list'>
<li>
<AddServiceContainer />
</li>
{ this.state.fetchingServices
? <div className="icon-spin5 animate-spin"></div>
: null }

</ul>
</div>
)
}
}

const mapStateToProps = (state) => {
return {
state
}
}

const mapDispatchToProps = (dispatch) => {
return {
getServices: (services) => { dispatch(getServices(services)) }
}
}

关于javascript - 在 promise 中 react setState 导致无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45358974/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com