最大调用堆栈大小超出响应

时间:2016-09-26 15:57:34

标签: reactjs react-redux

import React,{ Component } from 'react';
import { connect } from 'react-redux';
import { itemid } from '../actions/action';


class DetailPage extends Component{
    componentWillUpdate(){
    this.props.dispatch(itemid([this.props.params.item,`${this.props.params.item}${this.props.params.id}`]))
    }
    render(){
        const { dispatch } = this.props;
        return(
                <div>
                    {this.props.params.id}
                </div>
            )
    }
}
function selectstate(state){
    return{
        state
    }
}
export default connect(selectstate)(DetailPage)

// {this.props.params.item} 来自react-router(路径('/ detail / item / id'))

为什么我的调度是无限循环直到  错误(超出最大调用堆栈大小)

1 个答案:

答案 0 :(得分:3)

你正在componentWillUpdate开火。因此,每次更新状态时,您都会再次更新状态。你应该永远修改componentWillUpdate中的状态:https://facebook.github.io/react/docs/component-specs.html#updating-componentwillupdate

顺便说一句,在componentWillReceiveProps中执行此操作可能会做同样的事情,因为您的来自redux商店的状态将以​​props的形式出现。

问题是为什么你在更新后进行调度?是因为下面的功能设置状态?

如果您希望在设置特定状态后发送,则可以使用shouldComponentUpdate阻止除该状态以外的更新。但这感觉就像这种配置的副作用。我认为你在设置状态的同时调度你的事件要好得多,或者作为对它的回调。

相关问题